如何通过 C# 而不是 XAML 更改对象的 Y 投影轴和颜色 [英] How to change object's Y projection axis and color via C# rather than XAML
问题描述
我正在处理一个 Silverlight 项目,我将通过在 Y 投影轴上旋转矩形来翻转"矩形.我还将更改动画中间的颜色,以便矩形的背面看起来是不同的颜色.我可以在 XAML 中做到这一点没问题,但是我需要通过代码来做到这一点,因为我想动态翻转不同的矩形.我不想为网格上的每个矩形创建动画.这是我的 XAML 的样子:
I'm working on a Silverlight project where I will be 'flipping' rectangles by rotating them on the Y projection axis. I will also change to color in the middle of the animation so that it looks like the back side of the rectangle is a different color. I can do this in XAML no problem however I need to do this via code because I want to dynamically flip different rectangles. I don't want to have to create an animation for each rectangle on the grid. Here's what my XAML looks like:
<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 感到有些困惑.任何想法如何去做?
I've created storyboards from code several times however this one is leaving me a bit stumped with the EasingDoubleKeyFrames. Any ideas how to go about doing this?
推荐答案
这应该是第一个动画的翻译:
This should be the translation of the first animation:
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
类.
When using Storyboards
you further need to register the target in a namescope, i recommend reading the whole reference of the Storyboard
class.
这篇关于如何通过 C# 而不是 XAML 更改对象的 Y 投影轴和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!