Sdílet prostřednictvím


DiscreteColorKeyFrame

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

Animates from the Color value of the previous key frame to the Value of the current key frame, by using discrete values.

<DiscreteColorKeyFrame   .../>

Managed Equivalent

DiscreteColorKeyFrame

Remarks

A key frame defines a segment of the ColorAnimationUsingKeyFrames object to which it belongs. Each key frame has a target Value and KeyTime. The KeyTime specifies the time at which the key frame's Value should be reached. The animation progresses from the target value of the previous key frame to the target value of the current key frame. The animation starts when the previous key frame ends, and the animation ends when the key time of the current key frame is reached.

Discrete key frames, such as DiscreteColorKeyFrame, 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 suddenly changes to the target value.

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 object 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.

  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>

See Also

Reference