2012-04-16 67 views
1

我想用它,但它doesnt't的工作,我想創建一個瓷磚的動畫,在後面的代碼,或者如果你知道這個GOL項目,請給我寫如何使用故事板爲Stackpanel的邊距添加動畫?

Deployment.Current.Dispatcher.BeginInvoke(() => 
        { 
         while(true){ 
         Duration duration = new Duration(TimeSpan.FromSeconds(0.15)); 

         // Create two DoubleAnimations and set their properties. 
         DoubleAnimation myDoubleAnimation1 = new DoubleAnimation(); 

         myDoubleAnimation1.Duration = duration; 
         myDoubleAnimation1.From = -173 
         myDoubleAnimation1.To = 173; 
         Storyboard sb = new Storyboard(); 
         sb.Duration = duration; 

         sb.Children.Add(myDoubleAnimation1); 

         Storyboard.SetTarget(myDoubleAnimation1, image); 

         // Set the attached properties of Canvas.Left and Canvas.Top 
         // to be the target properties of the two respective DoubleAnimations. 
         Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath(StackPanel.MarginProperty)); 

         // Begin the animation. 
         sb.Begin();} 
        }); 
+0

邊距的類型是厚度 - 不是雙倍(這是使用雙動畫的原因)。您需要動畫Margin屬性的內容,這是一個結構,我不知道您是否可以這樣做。你想做什麼。 – 2012-04-16 20:32:28

+2

您是否真的在Storyboard之後的無限循環中創建Storyboard? – Clemens 2012-04-16 20:34:59

+0

可能重複[Animating Margin Bottom Silverlight](http://stackoverflow.com/questions/6507954/animating-margin-bottom-silverlight) – ColinE 2012-04-16 20:39:10

回答

3

使用ThicknessAnimation而不是DoubleAnimation。這幾乎是一樣的。

編輯:

如果你想使動畫無盡的使用Timeline.RepeatBehavior

myThicknessAnimation1.RepeatBehavior = RepeatBehavior.Forever; 
+0

謝謝你的LPL答案,你可以用XAML寫我嗎?請 – user1272388 2012-04-17 08:39:38

+0

請參閱我給你的ThicknessAnimation鏈接中的Examples部分。 – LPL 2012-04-17 10:01:12

1

這裏是工作的例子,如果有人需要:

//動畫餘地標籤,從右到左命名爲「標籤」。從300到0.

var sb = new Storyboard(); 
      var ta = new ThicknessAnimation(); 
      ta.BeginTime = new TimeSpan(0); 
      ta.SetValue(Storyboard.TargetNameProperty, "label"); 
      Storyboard.SetTargetProperty(ta, new PropertyPath(MarginProperty)); 

      ta.From = new Thickness(300, 30, 0, 0); 
      ta.To = new Thickness(0, 30, 0, 0); 
      ta.Duration = new Duration(TimeSpan.FromSeconds(3)); 

      sb.Children.Add(ta); 
      sb.Begin(this);