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

如何更改对象';通过C#而不是XAML的s Y投影轴和颜色,c#,wpf,silverlight,xaml,storyboard,C#,Wpf,Silverlight,Xaml,Storyboard,我正在进行一个Silverlight项目,我将通过在Y投影轴上旋转矩形来“翻转”它们。我也会在动画中间改变颜色,使它看起来像矩形的背面是一个不同的颜色。我可以在XAML中做到这一点没有问题,但是我需要通过代码来做到这一点,因为我想动态翻转不同的矩形。我不想为网格上的每个矩形创建动画。下面是我的XAML的外观: <Storyboard x:Name="Flip1"> <DoubleAnimationUsingKeyFrames Storyboard.Tar

我正在进行一个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难住了。你知道怎么做吗?

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

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"));

当使用
故事板时
您还需要在名称范围中注册目标,我建议您阅读整个类的引用

正如我所知,xaml中的每个标记都代表它的
。嵌套元素是
关键帧
-您需要将项目添加到此集合。我看不出有什么问题……你好,H.B.也许我只是不知道我在这里做什么。这就是我试过的。没有错误,但也没有结果…var anim=新的双动画使用关键帧();添加(新的EasingDoubleKeyFrame(){KeyTime=TimeSpan.FromSeconds(0),Value=0});添加(新的EasingDoubleKeyFrame(){KeyTime=TimeSpan.FromSeconds(0.5),Value=-90});添加(新的EasingDoubleKeyFrame(){KeyTime=TimeSpan.FromSeconds(0.6),Value=-90});添加(新的EasingDoubleKeyFrame(){KeyTime=TimeSpan.FromSeconds(1),Value=0});情节提要sb=新情节提要();添加(动画);故事板.SetTarget(动画,Rec1);Storyboard.SetTargetProperty(动画,新属性路径(“Projection.RotationY”);Rec1.MouseEnter+=(发送对象,MouseEventArgs f)=>sb.Begin();是的,它在XAML中工作,没有问题。XAML只花了我几分钟就完成了。事实证明,在代码中执行此操作是一项更复杂的工作。您能否尝试将PropertyPath更改为“(UIElement.Projection)。(PlaneProjection.RotationY)”并看看会发生什么?