如何使用故事板为Stackpanel的边距设置动画?

时间:2012-04-16 20:30:06

标签: c# wpf silverlight windows-phone-7

我想用它,但它不起作用,我想在后面的代码中创建一个平铺动画,或者如果你知道这个gol的项目,请写信给我

 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();}
                    });

2 个答案:

答案 0 :(得分:3)

使用ThicknessAnimation代替DoubleAnimation。它几乎是一样的。

修改

如果你想让动画无休止地使用Timeline.RepeatBehavior

myThicknessAnimation1.RepeatBehavior = RepeatBehavior.Forever;

答案 1 :(得分:2)

以下是工作示例,如果有人需要:

//Animate margin for Label, named "label" from right to left. from 300 to 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);