如何通过c#而不是XAML更改对象的Y投影轴和颜色

本文关键字:对象 投影 颜色 何通过 XAML | 更新日期: 2023-09-27 18:05:50

我正在做一个Silverlight项目,我将通过在Y投影轴上旋转矩形来"翻转"它们。我还将改变动画中间的颜色,这样看起来矩形的背面是不同的颜色。我可以在XAML中做到这一点,但我需要通过代码来实现,因为我想动态地翻转不同的矩形。我不想为网格上的每个矩形创建动画。下面是我的XAML的样子:

<Storyboard x:Name="Flip1">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="rectangle">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-90"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="-90"/>
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
                <EasingColorKeyFrame KeyTime="0:0:0.5" Value="Blue"/>
                <EasingColorKeyFrame KeyTime="0:0:0.6" Value="Red"/>
            </ColorAnimationUsingKeyFrames>
        </Storyboard>

我已经从代码中创建了几次故事板,但是这一次让我有点难住了EasingDoubleKeyFrames。有什么好主意吗?

如何通过c#而不是XAML更改对象的Y投影轴和颜色

这应该是第一个动画的翻译:

var anim = new DoubleAnimationUsingKeyFrames();
anim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromSeconds(0), Value = 0 });
anim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromSeconds(0.5), Value = -90 });
anim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromSeconds(0.6), Value = -90 });
anim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromSeconds(1), Value = 0 });
Storyboard.SetTarget(anim, rectangle);
Storyboard.SetTargetProperty(anim, new PropertyPath("Projection.RotationY"));

当使用Storyboards时,您进一步需要在命名范围中注册目标,我建议阅读Storyboard类的整个参考。

相关文章: