2009-08-18 70 views
1

我遇到了Silverlight 3的一個非常奇怪的問題。我定義了一個擴展方法來創建一個圍繞給定的DoubleAnimation的故事板並播放該動畫。我知道Silverlight中故事板的默認持續時間是1秒,但我想爲我的動畫更改。但是,當我設置不同的持續時間時,動畫將在指定的持續時間內播放,而不是改變動畫在指定時間內從頭到尾移動,然後停止播放。例如,如果我將某些東西從0,0移動到0,10並將持續時間設置爲.3s,則該項目只會移動到0.3。我無法想象這是通過設計。有什麼想法發生在這裏?故事板持續時間中斷動畫

這是我正在使用的代碼。 ConfigureStoryboard是圍繞動畫創建故事板的地方。我已經刪除了一些關於easing函數的代碼,以使其更具可讀性。

public static void BeginAnimation(
     this Transform transform, 
     DependencyProperty property, 
     DoubleAnimation animation, 
     EasingFunction function 
    ) 
    { 
     var storyboard = new Storyboard(); 
     ConfigureStoryboard(animation, storyboard, function); 
     Storyboard.SetTarget(storyboard, transform); 
     Storyboard.SetTargetProperty(
      storyboard, 
      new PropertyPath(property)); 

     storyboard.Begin(); 
    } 

    private static void ConfigureStoryboard(DoubleAnimation animation, Storyboard storyboard, EasingFunction function) 
    { 
     DoubleAnimation myAnimation = new DoubleAnimation(); 
     storyboard.Duration = animation.Duration; 
     myAnimation.From = animation.From; 
     myAnimation.To = animation.To; 

     storyboard.Children.Add(myAnimation); 
    } 

回答

1
private static void ConfigureStoryboard(DoubleAnimation animation, Storyboard storyboard, EasingFunction function) 
    { 
     DoubleAnimation myAnimation = new DoubleAnimation(); 
     myAnimation.Duration = animation.Duration; 
     myAnimation.From = animation.From; 
     myAnimation.To = animation.To; 

     storyboard.Children.Add(myAnimation); 
    } 

動畫需要有一個持續時間否則它獲得默認,並且由於故事板將只爲0.3秒運行,只有動畫的三分之一將停止之前運行。

+0

動畫的確有一個持續時間設置。如果我忘了設置它,故事板將運行1秒。持續時間設置爲<1秒,它只會在完成之前停止播放。 – oltman 2009-08-18 18:51:00

+1

您需要設置動畫的持續時間和故事板的持續時間。如果您有一個長度爲0.3秒的故事板,其中包含1秒長的動畫,則只會播放動畫的前0.3秒。 – KeithMahoney 2009-08-18 20:23:08

+0

完美!非常感謝你! – oltman 2009-08-18 20:41:33