从代码中使用 Storyboard 时的 WPF 动画问题 [英] WPF animation problem when using Storyboard from code

查看:22
本文介绍了从代码中使用 Storyboard 时的 WPF 动画问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作包含信息的扁平方形瓷砖 3D 旋转木马.我正在制作这个旋转木马的动画,当一个人按下下一个和上一个按钮时旋转.

I'm working on a 3D carousel of flat, square tiles that will contain information. I'm working on animating this carousel to rotate when a person presses Next and Previous buttons.

我通过在应用到轮播的 RotateTransform3D 的 Rotation 属性上使用 BeginAnimation 使其工作,但我似乎无法制作相同动画的 Storyboard 版本.我需要 Storyboard 版本的原因是 HandOffBehavior.Compose 参数,因为没有它,多次单击下一个和上一个按钮会导致轮播错位.

I've gotten it to work by using BeginAnimation on the Rotation property of the RotateTransform3D I applied to the carousel, but I can't seem to make a Storyboard version of the same animation work. The reason I need the Storyboard version is for the HandOffBehavior.Compose parameter because without it, multiple clicks of my next and previous buttons results in a misaligned carousel.

这是故事板的代码:

RotateTransform3D tempTransform = (RotateTransform3D)wheel.Transform;
AxisAngleRotation3D rotation = (AxisAngleRotation3D)tempTransform.Rotation;

Storyboard storyboard = new Storyboard();            
DoubleAnimation animation = new DoubleAnimation();
animation.By = defaultAngle;
animation.Duration = TimeSpan.FromSeconds(.5);

Storyboard.SetTarget(animation, rotation);
Storyboard.SetTargetProperty(animation, new PropertyPath("Angle"));
storyboard.Children.Add(animation);

storyboard.Duration = animation.Duration;            
storyboard.Begin(new FrameworkContentElement(), HandoffBehavior.Compose);

出于某种原因,这段代码完全没有结果.我遵循了我必须的例子,所以我很沮丧.任何帮助是极大的赞赏.如果我可以复制 HandOffBehavior.Compose,我也完全愿意使用 BeginAnimation.

For some reason, this code results in absolutely nothing. I followed the examples I had to the letter, so I am quite frustrated. Any help is greatly appreciated. I am also completely open to using BeginAnimation if I can replicate HandOffBehavior.Compose.

推荐答案

我的经验来自 2D 动画,但我想问题是一样的.

My experience comes from 2D animation, but I guess the problem is the same.

出于一些愚蠢的原因(可能与对 XAML 的不健康关注有关),Storyboard 只能通过按名称查找 Freezable 对象来制作动画.(请参阅故事板概述中的示例.)因此,尽管您提供了对当您调用 Storyboard.SetTarget(animation, rotation) 时,您的旋转"对象,Storyboard 只想记住并使用它没有的名称.

For some stupid reason (probably relating to an unhealthy focus on XAML), Storyboards can only animate Freezable objects by looking them up by name. (See example in Storyboards Overview.) Thus although you provide a reference to your 'rotation' object when you call Storyboard.SetTarget(animation, rotation), the Storyboard only wants to remember and use a name, which it does not have.

解决办法是:

  • 围绕将管理转换的元素创建一个命名范围.
  • 为每个正在设置动画的 Freezable 对象调用 RegisterName().
  • 将元素传递给 Storyboard.Begin()

这会使您的代码看起来像这样(未测试):

Which would make your code look something like this (not tested):

FrameworkContentElement element = new FrameworkContentElement();
NameScope.SetNameScope(element, new NameScope());

RotateTransform3D tempTransform = (RotateTransform3D)wheel.Transform;
AxisAngleRotation3D rotation = (AxisAngleRotation3D)tempTransform.Rotation;
element.RegisterName("rotation", rotation);

Storyboard storyboard = new Storyboard();            
DoubleAnimation animation = new DoubleAnimation();
animation.By = defaultAngle;
animation.Duration = TimeSpan.FromSeconds(.5);

Storyboard.SetTarget(animation, rotation);
Storyboard.SetTargetProperty(animation, new PropertyPath("Angle"));
storyboard.Children.Add(animation);

storyboard.Duration = animation.Duration;            
storyboard.Begin(element, HandoffBehavior.Compose);

这些在 XAML 中都不是必需的,因为您的对象是自动注册的.

None of this is necessary in XAML because your objects are automatically registered.

但后来我发现你可以通过完全省略故事板来简化事情:

But then I worked out that you can simplify things by leaving out the Storyboard altogether:

var T = new TranslateTransform(40, 0);
Duration duration = new Duration(new TimeSpan(0, 0, 0, 1, 0);
DoubleAnimation anim = new DoubleAnimation(30, duration);
T.BeginAnimation(TranslateTransform.YProperty, anim);

这篇关于从代码中使用 Storyboard 时的 WPF 动画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆