To create an object using scripting, see CreateFromXAML.
A key frame defines a segment of the DoubleAnimationUsingKeyFrames to which it belongs. Each key frame has a target Value and a KeyTime. The KeyTime specifies the time at which the key frame's Value should be reached. A key frame animates from the target value of the previous key frame to its own target value. It starts when the previous key frame ends and ends when its own key time is reached.
Discrete key frames like DiscreteDoubleKeyFrame create sudden "jumps" between values (no interpolation). The animated property does not change until the key frame's key time is reached at which point the animated property goes suddenly to the target value.
The following example moves a rectangle across a screen. The example uses the DoubleAnimationUsingKeyFrames class to animate the X property of a TranslateTransform applied to a Rectangle. This animation, which repeats indefinitely, uses three key frames in the following manner:
- During the first three seconds, uses an instance of the LinearDoubleKeyFrame class to move the rectangle along a path at a steady rate from its starting position to the 500 position. Linear key frames like LinearDoubleKeyFrame create a smooth linear transition between values.
- At the end of the fourth second, uses an instance of the DiscreteDoubleKeyFrame class to suddenly move the rectangle to the next position. Discrete key frames like DiscreteDoubleKeyFrame create sudden jumps between values. In this example, the rectangle is at the starting position and then suddenly appears at the 500 position.
- In the final two seconds, uses an instance of the SplineDoubleKeyFrame class to move the rectangle back to its starting position. Spline key frames like SplineDoubleKeyFrame create a variable transition between values according to the value of the KeySpline property. A KeySpline provides a way to alter the relationship of time versus value during the animation duration to be nonlinear, and in particular the relationship can be a curve that would be difficult to produce with individual keyframes. In this example, the rectangle begins by moving slowly and then speeds up exponentially toward the end of the time segment.
<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <Rectangle Fill="Blue" Width="50" Height="50"> <Rectangle.RenderTransform> <TranslateTransform x:Name="MyAnimatedTranslateTransform" X="0" Y="0" /> </Rectangle.RenderTransform> <Rectangle.Triggers> <EventTrigger RoutedEvent="Rectangle.Loaded"> <BeginStoryboard> <Storyboard> <!-- Animate the TranslateTransform's X property from 0 to 350, then 50, then 200 over 10 seconds. --> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="MyAnimatedTranslateTransform" Storyboard.TargetProperty="X" Duration="0:0:10"> <!-- Using a LinearDoubleKeyFrame, the rectangle moves steadily from its starting position to 500 over the first 3 seconds. --> <LinearDoubleKeyFrame Value="500" KeyTime="0:0:3" /> <!-- Using a DiscreteDoubleKeyFrame, the rectangle suddenly appears at 400 after the fourth second of the animation. --> <DiscreteDoubleKeyFrame Value="400" KeyTime="0:0:4" /> <!-- Using a SplineDoubleKeyFrame, the rectangle moves back to its starting point. The animation starts out slowly at first and then speeds up. This KeyFrame ends after the 6th second. --> <SplineDoubleKeyFrame KeySpline="0.6,0.0 0.9,0.00" Value="0" KeyTime="0:0:6" /> </DoubleAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> </Canvas>