[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Make a continuously spinning 3D cube with XAML and C#

continuously spinning 3D cube

This example is similar to the previous one Rotate a 3D cube using XAML and C# except it displays a continuously spinning 3D cube.

The basic structure of the program's 3D objects is similar to that of the previous example. The main differences are that the new Model3DGroup object has a name so the XAML code can refer to it and that the Model3DGroup now has a transformation. The following code shows the definition of the Model3DGroup object.

<Model3DGroup x:Name="CubeModel"> <Model3DGroup.Transform> <Transform3DGroup> <RotateTransform3D /> </Transform3DGroup> </Model3DGroup.Transform> ... </Model3DGroup>

The Window object uses the following code to define a Storyboard named RotateStoryboard.

<Window.Resources> <Storyboard x:Key="RotateStoryboard"> <Rotation3DAnimationUsingKeyFrames BeginTime="00:00:00" RepeatBehavior="Forever" Storyboard.TargetName="CubeModel" Storyboard.TargetProperty="(Model3D.Transform).(Transform3DGroup.Children)[0].(RotateTransform3D.Rotation)"> <SplineRotation3DKeyFrame KeyTime="00:00:02"> <SplineRotation3DKeyFrame.Value> <AxisAngleRotation3D Angle="180" Axis="0,1,0"/> </SplineRotation3DKeyFrame.Value> </SplineRotation3DKeyFrame> <SplineRotation3DKeyFrame KeyTime="00:00:04"> <SplineRotation3DKeyFrame.Value> <AxisAngleRotation3D Angle="359" Axis="0,1,0"/> </SplineRotation3DKeyFrame.Value> </SplineRotation3DKeyFrame> </Rotation3DAnimationUsingKeyFrames> </Storyboard> </Window.Resources>

The Storyboard contains a Rotation3DAnimationUsingKeyFrames object to modify a rotation. The target of the animation (the object that is animated) is CubeModel. The property that is animated is that object's (Model3D.Transform).(Transform3DGroup.Children)[0].(RotateTransform3D.Rotation) property. This mess (which was pretty hard to figure out) means go to the model's Transform property, find its third Transform3DGroup child, and modify the resulting object's RotateTransform3D.Rotation property.

The two SplineRotation3DKeyFrame objects inside the Rotation3DAnimationUsingKeyFrames give the values that the property should have at various times while the Storyboard is executing. In this example, the rotation should be 180 degrees rotated around the Y axis 2 seconds after the Storyboard starts and, 259 rotated degrees around the Y axis 4 seconds after the animation starts. Notice that the Storyboard object's RepeatBehavior is Forever so it repeats after it finishes.

Having defined the resource dictionary containing the Storyboard, the Window also defines a trigger to start the Storyboard when the window is loaded.

<Window.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <BeginStoryboard Storyboard="{StaticResource RotateModel}"/> </EventTrigger> </Window.Triggers>

This trigger starts when the framework element loads. It starts the Storyboard, and that animates the cube's rotation (forever).

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.