WPF 故事板动画不起作用

Raa*_*esh 2 c# 3d wpf animation storyboard

我有我的自定义 3D 模型类 (Model),它包含 Visual3D 元素和一个Storyboard(sb) 来保存与该模型相关的动画。我正在尝试使用 旋转 Visual3D 元素,Storyboard但不幸的是它不起作用。

这是代码片段

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

    }
Run Code Online (Sandbox Code Playgroud)

Cle*_*ens 5

这个问题在这个答案中有描述。

Storyboard.SetTarget您需要为转换和调用注册一个名称,而不是使用Storyboard.SetTargetName。此外,您必须调用Storyboard.Begin(FrameworkElement)并传递具有适当名称范围的 FrameworkElement 作为参数(this此处)。

RegisterName("RotateTransform", rotateTransform);
Storyboard.SetTargetName(rotateAnimation, "RotateTransform");
...
model.sb.Begin(this);
Run Code Online (Sandbox Code Playgroud)

另外我想你需要在某处清除故事板的孩子,或者在动画开始时创建一个新的故事板。