Proprietà ColorAnimationUsingKeyFrames.KeyFrames
Aggiornamento: novembre 2007
Ottiene o imposta l'insieme di oggetti ColorKeyFrame che definiscono l'animazione.
Assembly: PresentationCore (in PresentationCore.dll)
XMLNS per XAML: http://schemas.microsoft.com/winfx/xaml/presentation
/** @property */ public ColorKeyFrameCollection get_KeyFrames() /** @property */ public void set_KeyFrames(ColorKeyFrameCollection value)
public function get KeyFrames () : ColorKeyFrameCollection public function set KeyFrames (value : ColorKeyFrameCollection)
<object> <ColorKeyFrameCollection .../> </object>
Valore proprietà
Tipo: System.Windows.Media.Animation.ColorKeyFrameCollectionInsieme di oggetti ColorKeyFrame che definiscono l'animazione. Il valore predefinito è Empty.
In questo esempio viene illustrato come animare la proprietà Color di un oggetto SolidColorBrush utilizzando fotogrammi chiave.
Nell'esempio seguente viene utilizzata la classe ColorAnimationUsingKeyFrames per animare la proprietà Color di un oggetto SolidColorBrush. In questa animazione vengono utilizzati tre fotogrammi chiave nel modo seguente:
Durante i primi due secondi, viene utilizzata un'istanza della classe LinearColorKeyFrame per modificare gradualmente il colore da verde a rosso. I fotogrammi chiave lineari come LinearColorKeyFrame creano una transizione lineare uniforme tra valori.
Durante la fine del mezzo secondo successivo, viene utilizzata un'istanza della classe DiscreteColorKeyFrame per modificare rapidamente il colore da rosso a giallo. I fotogrammi chiave discreti come DiscreteColorKeyFrame creano modifiche improvvise tra valori, ovvero la modifica di colore in questa parte dell'animazione si verifica rapidamente e risulta immediatamente evidente.
Durante i due secondi finali, viene utilizzata un'istanza della classe SplineColorKeyFrame per cambiare nuovamente il colore, questa volta da giallo in verde. I fotogrammi chiave Spline come SplineColorKeyFrame creano una transizione variabile tra i valori a seconda dei valori della proprietà KeySpline. In questo esempio la modifica di colore inizia lentamente e aumenta di velocità in modo esponenziale verso la fine dell'intervallo di tempo.
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Shapes; using System.Windows.Media.Animation; using System.Windows.Media; namespace Microsoft.Samples.KeyFrameExamples { public class ColorAnimationUsingKeyFramesExample : Page { public ColorAnimationUsingKeyFramesExample() { Title = "BooleanAnimationUsingKeyFrames Example"; Background = Brushes.White; Margin = new Thickness(20); // Create a NameScope for this page so that // Storyboards can be used. NameScope.SetNameScope(this, new NameScope()); StackPanel myStackPanel = new StackPanel(); myStackPanel.Orientation = Orientation.Vertical; myStackPanel.HorizontalAlignment = HorizontalAlignment.Center; // Create the Border that is the target of the animation. SolidColorBrush animatedBrush = new SolidColorBrush(); animatedBrush.Color = Color.FromArgb(255, 0, 255, 0); Border myBorder = new Border(); // Set the initial color of the border to green. myBorder.BorderBrush = animatedBrush; myBorder.BorderThickness = new Thickness(28); myBorder.Padding = new Thickness(20); myStackPanel.Children.Add(myBorder); // Create a TextBlock to host inside the Border. TextBlock myTextBlock = new TextBlock(); myTextBlock.Text = "This example shows how to use the ColorAnimationUsingKeyFrames" + " to create an animation on the BorderBrush property of a Border."; myBorder.Child = myTextBlock; // Assign the Brush a name so that // it can be targeted by a Storyboard. this.RegisterName( "AnimatedBrush", animatedBrush); // Create a ColorAnimationUsingKeyFrames to // animate the BorderBrush property of the Button. ColorAnimationUsingKeyFrames colorAnimation = new ColorAnimationUsingKeyFrames(); colorAnimation.Duration = TimeSpan.FromSeconds(6); // Create brushes to use as animation values. Color redColor = new Color(); redColor = Color.FromArgb(255, 255, 0, 0); Color yellowColor = new Color(); yellowColor = Color.FromArgb(255, 255, 255, 0); Color greenColor = new Color(); greenColor = Color.FromArgb(255, 0, 255, 0); // Go from green to red in the first 2 seconds. LinearColorKeyFrame creates // a smooth, linear animation between values. colorAnimation.KeyFrames.Add( new LinearColorKeyFrame( redColor, // Target value (KeyValue) KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2.0))) // KeyTime ); // In the next half second, go to yellow. DiscreteColorKeyFrame creates a // sudden jump between values. colorAnimation.KeyFrames.Add( new DiscreteColorKeyFrame( yellowColor, // Target value (KeyValue) KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2.5))) // KeyTime ); // 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. colorAnimation.KeyFrames.Add( new SplineColorKeyFrame( greenColor, // Target value (KeyValue) KeyTime.FromTimeSpan(TimeSpan.FromSeconds(4.5)), // KeyTime new KeySpline(0.6, 0.0, 0.9, 0.0) // KeySpline ) ); // Set the animation to repeat forever. colorAnimation.RepeatBehavior = RepeatBehavior.Forever; // Set the animation to target the Color property // of the object named "AnimatedBrush". Storyboard.SetTargetName(colorAnimation, "AnimatedBrush"); Storyboard.SetTargetProperty( colorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); // Create a storyboard to apply the animation. Storyboard myStoryboard = new Storyboard(); myStoryboard.Children.Add(colorAnimation); // Start the storyboard when the Border loads. myBorder.Loaded += delegate(object sender, RoutedEventArgs e) { myStoryboard.Begin(this); }; Content = myStackPanel; } } }
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ThicknessAnimationUsingKeyFrames Example"> <StackPanel Orientation="Vertical" HorizontalAlignment="Center"> <Border Background="#99FFFFFF" BorderThickness="28" Margin="0,60,0,20" Padding="20" > <Border.BorderBrush> <SolidColorBrush x:Name="MyAnimatedBrush" Color="Green" /> </Border.BorderBrush> <Border.Triggers> <EventTrigger RoutedEvent="Border.Loaded"> <BeginStoryboard> <Storyboard> <!-- Animate from green to red using a linear key frame, from red to yellow using a discrete key frame, and from yellow back to green with a spline key frame. This animation repeats forever. --> <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Color" Storyboard.TargetName="MyAnimatedBrush" Duration="0:0:6" FillBehavior="HoldEnd" RepeatBehavior="Forever"> <ColorAnimationUsingKeyFrames.KeyFrames> <!-- Go from green to red in the first 2 seconds. LinearColorKeyFrame creates a smooth, linear animation between values. --> <LinearColorKeyFrame Value="Red" KeyTime="0:0:2" /> <!-- In the next half second, go to yellow. DiscreteColorKeyFrame creates a sudden jump between values. --> <DiscreteColorKeyFrame Value="Yellow" KeyTime="0:0: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="0:0:4.5" KeySpline="0.6,0.0 0.9,0.00" /> </ColorAnimationUsingKeyFrames.KeyFrames> </ColorAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Border.Triggers> <TextBlock> This example shows how to use the ColorAnimationUsingKeyFrames to create an animation on the BorderBrush property of a Border. </TextBlock> </Border> </StackPanel> </Page>
Per l'esempio completo, vedere Esempio di animazione con fotogrammi chiave.
Windows Vista
.NET Framework e .NET Compact Framework non supportano tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.