Compartir a través de


ColorAnimationUsingKeyFrames

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Animates the value of a Color along a set of KeyFrames over a specified Duration.

<ColorAnimationUsingKeyFrames>
  oneOrMoreColorKeyFrames
</ColorAnimationUsingKeyFrames>

XAML Values

Value

Description

oneOrMoreColorKeyFrames

One or more ColorKeyFrame object elements that define the key frames for the animation. These can be any combination of LinearColorKeyFrame, DiscreteColorKeyFrame, and SplineColorKeyFrame object elements. Object elements defined here become members of the KeyFrames collection when scripting accesses the KeyFrames property at run time.

Managed Equivalent

ColorAnimationUsingKeyFrames

Remarks

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 properties. When the animation runs, it progresses from one key value to the next at the specified key times.

Unlike a ColorAnimation object, a ColorAnimationUsingKeyFrames object can have more than two target values; each key frame can potentially represent a different value as the animation progresses on the overall timeline. You can also control the interpolation method of individual ColorKeyFrame segments. There are three types of ColorKeyFrame classes, one for each supported interpolation method: LinearColorKeyFrame, DiscreteColorKeyFrame, and SplineColorKeyFrame.

When you declare a ColorAnimationUsingKeyFrames in XAML, the order of the ColorKeyFrame object elements is not significant, because the KeyTime property 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.

For more information on basic concepts, see Key-Frame Animations. Note that the Key-Frame Animations topic is written primarily for users of the managed API, and may not have code examples or specific information that address the JavaScript API scenarios.

Example

The following example uses the ColorAnimationUsingKeyFrames class to animate the Background property of a Canvas object. This animation uses three key frames in the following manner:

  1. During the first two seconds, LinearColorKeyFrame gradually changes the color from green to red. Linear key frames, such as LinearColorKeyFrame, create a smooth linear transition between values.

  2. During the end of the next half second, DiscreteColorKeyFrame quickly changes the color from red to yellow. Discrete key frames, such as DiscreteColorKeyFrame, create sudden changes between values; the animation occurs quickly and has no interpolation between values at all.

  3. During the final two seconds, SplineColorKeyFrame changes the color again, this time from yellow back to green. Spline key frames, such as SplineColorKeyFrame, create a variable transition between values according to the values of the KeySpline property. A KeySpline provides a nonlinear way to alter the relationship of time versus value during the animation duration. 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.

<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  Width="200" Height="200"
  Background="White"
  x:Name="Page">
  <Canvas.Triggers>
    <EventTrigger RoutedEvent="Canvas.Loaded">
      <BeginStoryboard>
        <Storyboard x:Name="ColorStoryboard">
          <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Page" 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>
      </BeginStoryboard>
    </EventTrigger>
  </Canvas.Triggers>
</Canvas>