当从code。使用故事板WPF动画的问题 [英] WPF animation problem when using Storyboard from code

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

问题描述

我的工作平面,广场砖,将包含信息的3D旋转木马。我正在动画这个旋转木马旋转,当一个人presses下一步,previous按钮。

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.

我已经得到它使用BeginAnimation在RotateTransform3D我施加到转盘的旋转性的工作,但我似乎无法做出同样的动画作品的故事板的版本。我需要的故事板版本的原因是为HandOffBehavior.Compose参数,因为没有它,我的下一个和previous按钮导致错位旋转木马多次点击。

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.

下面是code的情节提要:

Here is the code for the Storyboard:

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

由于某些原因,在绝对没有这个code结果。我跟着我不得不信的例子,让我感到很沮丧。任何帮助是极大的AP preciated。我也用BeginAnimation完全开放的,如果我可以复制HandOffBehavior.Compose。

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.SetTarget(动画,旋转),故事板只想记住和使用一个名称,它没有。

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.

解决的办法是:

  • 创建,将治理改造元件的周围的命名范围。
  • 呼叫RegisterName()用于进行动画每个可冻结对象。
  • 传递元素Storyboard.Begin()

这将使你的code看起来像这样(未测试):

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

这篇关于当从code。使用故事板WPF动画的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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