ColorAnimation class

Applies to Windows and Windows Phone

Animates the value of a Color property between two target values using linear interpolation over a specified Duration.

Inheritance

Object
  DependencyObject
    Timeline
      ColorAnimation

Syntax


public sealed class ColorAnimation : Timeline


<ColorAnimation .../>

Attributes

[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]
[WebHostHidden()]

Members

The ColorAnimation class has these types of members:

Constructors

The ColorAnimation class has these constructors.

ConstructorDescription
ColorAnimation Initializes a new instance of the ColorAnimation class.

 

Events

The ColorAnimation class has these events.

EventDescription
Completed Occurs when the Storyboard object has completed playing. (Inherited from Timeline)

 

Methods

The ColorAnimation class has these methods. It also inherits methods from the Object class.

MethodDescription
ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject)
GetAnimationBaseValue Returns any base value established for a dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject)
GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject)
ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject)
SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject)

 

Properties

The ColorAnimation class has these properties.

PropertyAccess typeDescription

AutoReverse

Read/writeGets or sets a value that indicates whether the timeline plays in reverse after it completes a forward iteration. (Inherited from Timeline)

BeginTime

Read/writeGets or sets the time at which this Timeline should begin. (Inherited from Timeline)

By

Read/writeGets or sets the total amount by which the animation changes its starting value.

ByProperty

Read-onlyIdentifies the By dependency property.

Dispatcher

Read-onlyGets the CoreDispatcher that this object is associated with. (Inherited from DependencyObject)

Duration

Read/writeGets or sets the length of time for which this timeline plays, not counting repetitions. (Inherited from Timeline)

EasingFunction

Read/writeGets or sets the easing function applied to this animation.

EasingFunctionProperty

Read-onlyIdentifies the EasingFunction dependency property.

EnableDependentAnimation

Read/writeGets or sets a value that declares whether animated properties that are considered dependent animations should be permitted to use this animation declaration.

EnableDependentAnimationProperty

Read-onlyIdentifies the EnableDependentAnimation dependency property.

FillBehavior

Read/writeGets or sets a value that specifies how the animation behaves after it reaches the end of its active period. (Inherited from Timeline)

From

Read/writeGets or sets the animation's starting value.

FromProperty

Read-onlyIdentifies the From dependency property.

RepeatBehavior

Read/writeGets or sets the repeating behavior of this timeline. (Inherited from Timeline)

SpeedRatio

Read/writeGets or sets the rate, relative to its parent, at which time progresses for this Timeline. (Inherited from Timeline)

To

Read/writeGets or sets the animation's ending value.

ToProperty

Read-onlyIdentifies the To dependency property.

 

Remarks

Use ColorAnimation to animate the property value of any dependency property that is of type Color.

Linear interpolation for a Color means that each of the ARGB values is treated as a byte and the interpolation is simply a mathematical operation. You get best results from color interpolation if at least one of the RGB components is the same or close to the same in both the starting value and ending value.

You usually need to use indirect property targeting in order to target a sub-property of another object that's the value of a property on the target. This is because very few properties that display color information in UI elements are actually of type Color. Most are instead of type Brush. To use ColorAnimation on UI elements, you typically are targeting the Color property of a SolidColorBrush that's the sub-property value. Syntax for this is shown in the XAML example in the "Examples" section. For more info on indirect property targeting and other storyboarded animation concepts, see Storyboarded animations or Property-path syntax.

A ColorAnimation typically has at least one of the From, By or To properties set, but never all three.

  • From only: The animation progresses from the value specified by the From property to the base value of the property being animated.
  • From and To: The animation progresses from the value specified by the From property to the value specified by the To property.
  • From and By: The animation progresses from the value specified by the From property to the value specified by the sum of the From and By properties.
  • To only: The animation progresses from the animated property's base value or a previous animation's output value to the value specified by the To property.
  • By only: The animation progresses from the base value of the property being animated or a previous animation's output value to the sum of that value and the value specified by the By property.

The From, By and To properties of a ColorAnimation aren't strictly a Color. Instead these are a Nullable for Color. The default value for these is null, not an uninitialized structure. That null value is how the animation system distinguishes that you haven't specifically set a value. Visual C++ component extensions (C++/CX) doesn't have a Nullable type, so it uses IReference instead.

Examples

The following example shows how to use ColorAnimation to animate the background color of a StackPanel.


<StackPanel x:Name="myStackPanel" Background="Red"
  Loaded="Start_Animation">
  <StackPanel.Resources>
    <Storyboard x:Name="colorStoryboard">

      <!-- Animate the background color of the canvas from red to green
        over 4 seconds. -->
      <ColorAnimation Storyboard.TargetName="myStackPanel" 
        Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
        From="Red" To="Blue" Duration="0:0:4"/>

    </Storyboard>
  </StackPanel.Resources>
</StackPanel>



// Start the animation when the object loads.
private void Start_Animation(object sender, RoutedEventArgs e)
{
    colorStoryboard.Begin();
}


Notice in the example above that the property value being animated (Color), belongs to a SolidColorBrush object that is not named or even explicitly declared. This indirect targeting is accomplished using the special syntax below.


Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"

Alternatively, you could explicitly create the SolidColorBrush, name it, and target its Color property directly. The example below shows how to create the same animation as the previous one except it uses direct property targeting.


<StackPanel Loaded="Start_Animation">
  <StackPanel.Resources>
    <Storyboard x:Name="colorStoryboard">
      <!-- Animate the background color of the canvas from red to green
        over 4 seconds. -->
      <ColorAnimation Storyboard.TargetName="mySolidColorBrush"
        Storyboard.TargetProperty="Color" From="Red" To="Blue" Duration="0:0:4"/>
    </Storyboard>
  </StackPanel.Resources>

  <StackPanel.Background>
    <SolidColorBrush x:Name="mySolidColorBrush" Color="Red" />
  </StackPanel.Background>

</StackPanel>



// Start the animation when the object loads.
private void Start_Animation(object sender, RoutedEventArgs e)
{
    colorStoryboard.Begin();
}


Requirements

Minimum supported client

Windows 8 [Windows Store apps only]

Minimum supported server

Windows Server 2012 [Windows Store apps only]

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml.Media.Animation
Windows::UI::Xaml::Media::Animation [C++]

Metadata

Windows.winmd

See also

Storyboarded animations
XAML animation sample
Timeline
SolidColorBrush
LinearGradientBrush
Color
Colors

 

 

Show:
© 2014 Microsoft