簡體   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