2013-02-10 92 views
1

我有我的自定義3D模型類(Model),其中包含Visual3D元素和Storyboard(sb)以保存與該模型相關的動畫。我試圖旋轉使用Storyboard的Visual3D元素,但不幸的是它不起作用。WPF 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(); 

    } 

回答

3

問題在this answer進行說明。

而不是使用Storyboard.SetTarget您需要爲變換註冊一個名稱並呼叫Storyboard.SetTargetName。此外,你必須調用Storyboard.Begin(FrameworkElement)並傳遞一個FrameworkElement作爲參數(這裏的this)。

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

而且我猜你需要的地方清除故事板的兒童,或創建每當動畫開始一個新的故事板。

+0

它工作。非常感謝克萊門斯花時間回覆! – Raathigesh 2013-02-10 11:51:15