2010-07-31 40 views
3

我正在研究包含信息的平面,方形瓷砖的3D轮播。当人们按下下一个按钮和上一个按钮时,我正在将这个轮播动画设置为旋转。从代码中使用Storyboard时的WPF动画问题

我已经通过在RotateTransform3D的Rotation属性上使用BeginAnimation将它应用于旋转木马,但似乎无法制作同一动画作品的Storyboard版本。我需要Storyboard版本的原因是HandOffBehavior.Compose参数,因为没有它,多次单击我的下一个和上一个按钮会导致旋转木马未对齐。

这里是故事板代码:

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。

回答

1

我的经验来自2D动画,但我想问题是相同的。

对于一些愚蠢的原因(可能与对XAML的不健康关注)有关,故事板只能通过按名称查找Freezable对象来设置动画。 (请参见Storyboards Overview中的示例。)因此,尽管您在调用Storyboard.SetTarget(动画,旋转)时提供了对“旋转”对象的引用,故事板只会记住并使用它没有的名称。

解决的办法是:

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

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

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的对象是自动注册。

编辑:但后来我工作了,你可以通过简化干脆离开了故事板的东西:

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