具有多个ColorAnimations的Silverlight情节提要

本文关键字:Silverlight ColorAnimations | 更新日期: 2023-09-27 18:29:22

我很难弄清楚如何使用ColorAnimation沿可见光谱更改椭圆的填充颜色。ColorAnimation将颜色混合在一起,而不是沿着光谱移动,所以我想出了以下方法。

<Ellipse x:Name="indicatorEllipse" HorizontalAlignment="Right" VerticalAlignment="Center" Height="20" Width="20" Stroke="Black" Margin="0 0 5 0" >
<Ellipse.Resources>
    <Storyboard x:Name="indicatorStoryboard">
        <!-- Animate the fill color of the Ellipse from red to green over 100 seconds. -->
        <ColorAnimation BeginTime="00:00:00" Storyboard.TargetName="indicatorColorBrush" 
Storyboard.TargetProperty="Color"
From="Red" To="OrangeRed" Duration="0:00:14" />
        <ColorAnimation BeginTime="00:00:15" Storyboard.TargetName="indicatorColorBrush" 
Storyboard.TargetProperty="Color"
From="OrangeRed" To="Orange" Duration="0:00:14" />
        <ColorAnimation BeginTime="00:00:30" Storyboard.TargetName="indicatorColorBrush" 
Storyboard.TargetProperty="Color"
From="Orange" To="Yellow" Duration="0:00:30" />
        <ColorAnimation BeginTime="00:01:01" Storyboard.TargetName="indicatorColorBrush" 
Storyboard.TargetProperty="Color"
From="Yellow" To="YellowGreen" Duration="0:00:14" />
        <ColorAnimation BeginTime="00:01:16" Storyboard.TargetName="indicatorColorBrush" 
Storyboard.TargetProperty="Color"
From="YellowGreen" To="GreenYellow" Duration="0:00:14" />
        <ColorAnimation BeginTime="00:01:31" Storyboard.TargetName="indicatorColorBrush" 
Storyboard.TargetProperty="Color"
From="GreenYellow" To="Green" Duration="0:00:14" />
    </Storyboard>
</Ellipse.Resources>
<Ellipse.Fill>
    <SolidColorBrush x:Name="indicatorColorBrush" Color="Red" />
</Ellipse.Fill>

这行不通!这将导致以下错误。。。

同一包含情节提要中的多个动画不能作为目标单个元素上的相同属性。

有人对如何做到这一点有想法吗?

具有多个ColorAnimations的Silverlight情节提要

ColorAnimationUsingKeyFrames将解决您的问题:

<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Color" Storyboard.TargetName="indicatorColorBrush">
    <SplineColorKeyFrame KeyTime="0:0:2" Value="OrangeRed"/>
    <SplineColorKeyFrame KeyTime="0:0:4" Value="Orange"/>
    <SplineColorKeyFrame KeyTime="0:0:6" Value="Yellow"/>
</ColorAnimationUsingKeyFrames>

我还建议尝试一下Expression Blend,它可以让处理动画变得容易得多。