我在Storyboard方法中做了类似的事情,但无法达到预期的效果.这个动画我想在页面加载后播放.
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>
最佳答案 您正在设置source.RenderTransform两次.第一个变换动画引用了一个CompositeTransform对象,该对象在动画运行时已从UIElement中删除.
摆脱第二个来源.RenderTransform = new CompositeTransform();你应该得到更好的结果.
“剪切和粘贴的来源是很多编码邪恶的来源”:)