繁体   English   中英

WPF故事板动画不起作用

[英]WPF Storyboard Animation Not Working

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

这是代码片段

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.SetTarget您需要注册转换的名称并调用Storyboard.SetTargetName 此外,您必须调用Storyboard.Begin(FrameworkElement)并传递带有适当名称范围的FrameworkElement作为参数(此处this参数)。

RegisterName("RotateTransform", rotateTransform);
Storyboard.SetTargetName(rotateAnimation, "RotateTransform");
...
model.sb.Begin(this);

另外,我猜您需要在某个地方清除情节提要的子级,或者在动画开始时创建一个新的情节提要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM