如何使用情节提要对堆栈面板的边距进行动画处理

本文关键字:处理 动画 用情 何使 堆栈 | 更新日期: 2023-09-27 18:31:29

我想使用它,但它不起作用,我想在代码隐藏中创建平铺动画,或者如果您知道这个 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();}
                    });

如何使用情节提要对堆栈面板的边距进行动画处理

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

//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);

使用ThicknessAnimation而不是DoubleAnimation。这几乎是一样的。

编辑:

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

myThicknessAnimation1.RepeatBehavior = RepeatBehavior.Forever;