Namespace:
System.Windows.Media.Animation
Assembly:
System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
<ContentPropertyAttribute("KeyFrames", True)> _
Public NotInheritable Class ColorAnimationUsingKeyFrames _
Inherits Timeline
Dim instance As ColorAnimationUsingKeyFrames
[ContentPropertyAttribute("KeyFrames", true)]
public sealed class ColorAnimationUsingKeyFrames : Timeline
XAML Object Element Usage
<ColorAnimationUsingKeyFrames ...>
oneOrMoreColorKeyFrames
</ColorAnimationUsingKeyFrames>
A key frame animation's target values are defined by its KeyFrames property, which contains a collection of ColorKeyFrame objects. Each ColorKeyFrame defines a segment of the animation with its own target Value and KeyTime. When the animation runs, it progresses from one key value to the next at the specified key times.
There are three types of ColorKeyFrame classes, one for each supported interpolation method: LinearColorKeyFrame, DiscreteColorKeyFrame, and SplineColorKeyFrame.
Unlike a ColorAnimation, a ColorAnimationUsingKeyFrames can have more than two target values. You can also control the interpolation method of individual ColorKeyFrame segments.
When declaring a ColorAnimationUsingKeyFrames in XAML, the order of the ColorKeyFrame object elements is not significant, because the KeyTime controls the timing and therefore the order in which the key frames are executed. However, it is good markup style to keep the element order the same as the KeyTime sequence order.
The following example uses the ColorAnimationUsingKeyFrames class to animate the Background property of a StackPanel. This animation uses three key frames in the following manner:
During the first two seconds, LinearColorKeyFrame gradually changes the color from green to red. Linear key frames like LinearColorKeyFrame create a smooth linear transition between values.
During the end of the next half second, DiscreteColorKeyFrame quickly changes the color from red to yellow. Discrete key frames like DiscreteColorKeyFrame create sudden changes between values; the animation occurs quickly and has no interpolation between values at all.
During the final two seconds, SplineColorKeyFrame changes the color again, this time from yellow back to green. Spline key frames like SplineColorKeyFrame create a variable transition between values according to the values 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 key frames. In this example, the change in color begins slowly and speeds up exponentially toward the end of the time segment.
Run this sample
<StackPanel Background="White" x:Name="myStackPanel"
Loaded="Start_Animation">
<StackPanel.Resources>
<Storyboard x:Name="colorStoryboard">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00"
Storyboard.TargetName="myStackPanel"
Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
<!-- Go from green to red in the first 2 seconds. LinearColorKeyFrame creates
a smooth, linear animation between values. -->
<LinearColorKeyFrame Value="Red" KeyTime="00:00:02" />
<!-- In the next half second, go to yellow. DiscreteColorKeyFrame creates a
sudden jump between values. -->
<DiscreteColorKeyFrame Value="Yellow" KeyTime="00:00:2.5" />
<!-- In the final 2 seconds of the animation, go from yellow back to green. SplineColorKeyFrame
creates a variable transition between values depending on the KeySpline property. In this example,
the animation starts off slow but toward the end of the time segment, it speeds up exponentially.-->
<SplineColorKeyFrame Value="Green" KeyTime="00:00:4.5" KeySpline="0.6,0.0 0.9,0.00" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</StackPanel.Resources>
</StackPanel>
' Start the animation when the object loads
Private Sub Start_Animation(ByVal sender As Object, ByVal e As EventArgs)
colorStoryboard.Begin()
End Sub
// Start the animation when the object loads
private void Start_Animation(object sender, EventArgs e)
{
colorStoryboard.Begin();
}
System..::.Object
System.Windows..::.DependencyObject
System.Windows.Media.Animation..::.Timeline
System.Windows.Media.Animation..::.ColorAnimationUsingKeyFrames
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference
Other Resources