开发者

How to change object's Y projection axis and color via C# rather than XAML

开发者 https://www.devze.com 2023-03-28 17:38 出处:网络
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 lik

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>
   开发者_如何学JAVA     </Storyboard>

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

When using Storyboards you further need to register the target in a namescope, i recommend reading the whole reference of the Storyboard class.

0

精彩评论

暂无评论...
验证码 换一张
取 消