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.
精彩评论