<StackPanel Loaded="Start_Animation" Background="White" x:Name="stackPanel">
<StackPanel.Resources>
<Storyboard x:Name="colorStoryboard">
<ColorAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetName="stackPanel" 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="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.0"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</StackPanel.Resources>
</StackPanel>
本文介绍了一种使用XAML创建颜色动画的方法。通过定义Storyboard及其内部的ColorAnimationUsingKeyFrames,可以实现背景颜色从绿色平滑过渡到红色,然后突然变为黄色,并最终以一种先慢后快的方式回到绿色的效果。
1408

被折叠的 条评论
为什么被折叠?



