WPF 故事板动画不工作 [英] WPF Storyboard Animation Not Working

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

问题描述

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

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.

这是代码片段

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

    }

推荐答案

这个答案中描述了该问题.

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

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

<小时>

另外,我猜您需要在某处清除 Storyboard's Children,或者在动画开始时创建一个新的 Storyboard.


Also I guess you need to clear the Storyboard's Children somewhere, or create a new Storyboard whenever the animation is started.

这篇关于WPF 故事板动画不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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