2012-08-01 69 views
2

我有這段代碼,不知道如何禁用動畫。如何禁用WPF動畫

任何線索?謝謝!

 #region Blinking Animation 
      Storyboard sb = new Storyboard(); 
      if (IsImageBlinking) 
      { 
       DoubleAnimation da = new DoubleAnimation(); 

       da.From = 1.0; 
       da.To = 0.0; 
       da.RepeatBehavior = RepeatBehavior.Forever; 
       da.AutoReverse = true; 


       sb.Children.Add(da); 
       Storyboard.SetTargetProperty(da, new PropertyPath("(Image.Opacity)")); 
       Storyboard.SetTarget(da, image1); 
       sb.Begin(); 
      } 
      else // This code doesn't disable the animation :(
      { 
//!!!! Here I need to disable the animation. 
       sb.Stop(); 
       sb.Children.Clear(); 
      } 

      #endregion 
+0

什麼時候你想禁用動畫? – lionheart 2012-08-01 13:52:38

+0

@lionheart after else // – 2012-08-01 13:55:10

+2

您是否意識到每次調用此代碼時都會創建* new * Storyboard的事實? – Clemens 2012-08-01 13:56:09

回答

5

它的工作原理,如果你使用相同故事板實例BeginStop。聲明sb作爲類的成員:

public class MainWindow 
{ 
    private Storyboard sb = new Storyboard(); 

    ... 
} 
+0

一旦你有故事板領域,你可以調用方法'開始()'和'停止()'開始/停止動畫。 – Manfred 2013-05-18 12:33:55

+0

@Manfred OP似乎已經知道...... – Clemens 2013-05-19 19:00:38

2

您是否嘗試過使用sb.BeginAnimation(Image.OpacityProperty, null);代替sb.stop()