2012-04-16 62 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);