WPF动画故事板不工作 [英] WPF Storyboard Animation Not Working
问题描述
我有我的,其中包含的Visual3D元素和故事板
(SB)持有与该模型的动画自定义的3D模型类(模型)。我试图使用故事板
旋转的Visual3D元素,但遗憾的是它不工作。
下面是code段
公共无效AnimationRotate(型号机型,双期限,双重的startTime,的repeatBehavior行为)
{
//旋转变换3D
RotateTransform3D rotateTransform =新RotateTransform3D();
//指定变换到模型
model.Visual3D.Transform = Transform3DHelper.CombineTransform(model.Visual3D.Transform,rotateTransform);
//定义旋转轴
AxisAngleRotation3D rotateAxis =新AxisAngleRotation3D(新的Vector3D(0,0,1),180);
//创建3D旋转动画
Rotation3DAnimation rotateAnimation =新Rotation3DAnimation(rotateAxis,TimeSpan.FromSeconds(0.5));
//旋转行为
rotateAnimation.RepeatBehavior =行为;
//从时间开始动画
rotateAnimation.BeginTime = TimeSpan.FromSeconds(startTime时);
//开头动画 - 这工作正常
// rotateTransform.BeginAnimation(RotateTransform3D.RotationProperty,rotateAnimation);
Storyboard.SetTargetProperty(rotateAnimation,新的PropertyPath(RotateTransform3D.RotationProperty));
Storyboard.SetTarget(rotateAnimation,rotateTransform);
//添加动画模型的故事板
model.sb.Children.Add(rotateAnimation);
//但这种方法是不是WOKRING
model.sb.Begin();
}
这个问题在描述这个答案。
而不是使用 Storyboard.SetTarget
您需要的转换和调用 Storyboard.SetTargetName
注册一个名称。此外,你必须调用 Storyboard.Begin(FrameworkElement的)并传递一个FrameworkElement的有适当的名称范围为参数(这
点击这里)。
RegisterName(RotateTransform,rotateTransform);
Storyboard.SetTargetName(rotateAnimationRotateTransform);
...
model.sb.Begin(本);
此外,我想你需要清除故事板的儿童的地方,或创建一个新的故事板,只要动画开始。
I have my custom 3D model class (Model) which contains Visual3D element and a Storyboard
(sb) to hold animations related to that model. I'm trying to rotate the Visual3D element using the Storyboard
but unfortunately it's not working.
Here is the code snippet
public void AnimationRotate(Model model, double duration, double startTime, RepeatBehavior behaviour)
{
//Rotate transform 3D
RotateTransform3D rotateTransform = new RotateTransform3D();
//assign transform to the model
model.Visual3D.Transform = Transform3DHelper.CombineTransform(model.Visual3D.Transform, rotateTransform);
//define the rotation axis
AxisAngleRotation3D rotateAxis = new AxisAngleRotation3D(new Vector3D(0, 0, 1), 180);
//create 3D rotation animation
Rotation3DAnimation rotateAnimation = new Rotation3DAnimation(rotateAxis, TimeSpan.FromSeconds(0.5));
//rotation behaviour
rotateAnimation.RepeatBehavior = behaviour;
//start animation from time
rotateAnimation.BeginTime = TimeSpan.FromSeconds(startTime);
//begin animation - THIS WORKS FINE
// rotateTransform.BeginAnimation(RotateTransform3D.RotationProperty, rotateAnimation);
Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath(RotateTransform3D.RotationProperty));
Storyboard.SetTarget(rotateAnimation, rotateTransform);
//add animation to the storyboard of the model
model.sb.Children.Add(rotateAnimation);
//BUT THIS APPROACH IS NOT WOKRING
model.sb.Begin();
}
The problem is described in this answer.
Instead of using Storyboard.SetTarget
you need to register a name for the transform and call Storyboard.SetTargetName
. Furthermore you must call Storyboard.Begin(FrameworkElement) and pass a FrameworkElement with the appropriate name scope as parameter (this
here).
RegisterName("RotateTransform", rotateTransform);
Storyboard.SetTargetName(rotateAnimation, "RotateTransform");
...
model.sb.Begin(this);
Also I guess you need to clear the Storyboard's Children somewhere, or create a new Storyboard whenever the animation is started.
这篇关于WPF动画故事板不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!