4
我在Storyboard方法中做了这样的事情,但无法达到预期的效果。这个动画我想在页面加载后播放。如何同时为两个轴(X,Y)做翻译动画?
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
CreateTranslateAnimation(image1);
}
private void CreateTranslateAnimation(UIElement source)
{
Storyboard sb = new Storyboard();
DoubleAnimationUsingKeyFrames animationFirstX = new DoubleAnimationUsingKeyFrames();
source.RenderTransform = new CompositeTransform();
Storyboard.SetTargetProperty(animationFirstX, new PropertyPath(CompositeTransform.TranslateXProperty));
Storyboard.SetTarget(animationFirstX, source.RenderTransform);
animationFirstX.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = kt1, Value = 20 });
DoubleAnimationUsingKeyFrames animationFirstY = new DoubleAnimationUsingKeyFrames();
source.RenderTransform = new CompositeTransform();
Storyboard.SetTargetProperty(animationFirstY, new PropertyPath(CompositeTransform.TranslateYProperty));
Storyboard.SetTarget(animationFirstY, source.RenderTransform);
animationFirstY.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = kt1, Value = 30 });
sb.Children.Add(animationFirstX);
sb.Children.Add(animationFirstY);
sb.Begin();
}
长话短说...
我想写的.cs代码相当于该代码
<Storyboard x:Name="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="image1">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="20"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="image1">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="30"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
这是什么做的是不是你想要的?请提供一个完整的示例,显示您如何使用此代码。 – 2012-03-15 22:15:32
这只是沿y轴移动图像,而我要求 写这段代码是沿着两个轴,像在一个诊断中移动它。时尚,从(0,0)到(20,30)都是合作伙伴。在 animationFirstX.KeyFrames.Add给出(新EasingDoubleKeyFrame(){KeyTime = KT1,值= 20} animationFirstY.KeyFrames.Add(新EasingDoubleKeyFrame(){KeyTime = KT1,值= 30} 这些线(见代码) – user1235555 2012-03-16 06:03:13
你也可以在xaml中定义你的动画,然后在运行时访问你的代码并修改它的道具。 – Vitalij 2012-03-26 03:04:31