学海无涯

导航

动画 Animation

using System.Windows.Media.Animation;
<Grid>
        <StackPanel>
            <Button x:Name="btn" Content="执行动画" Width="100" Height="40" Click="Button_Click"/>
        </StackPanel>
    </Grid>

 

  private void Button_Click(object sender, RoutedEventArgs e)
        {
            //创建一个双精度的动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.By = -30;//设置范围,在原来的基础上减30
            //animation.From = btn.Width;//动画开始值
            //animation.To = btn.Width - 30;//动画结束值
            animation.Duration = TimeSpan.FromSeconds(3);//动画的持续时间
            animation.AutoReverse = true;//执行完动画之后,恢复到原始状态
            //animation.RepeatBehavior = RepeatBehavior.Forever;//动画一直持续执行
            //animation.RepeatBehavior = new RepeatBehavior(3);//动画执行3次

            animation.Completed += Animation_Completed;
            //在当前按钮上执行该动画
            btn.BeginAnimation(Button.WidthProperty, animation);
        }

        private void Animation_Completed(object? sender, EventArgs e)
        {
            btn.Content = "动画已完成";
        }

  

 

posted on 2022-12-14 10:42  宁静致远.  阅读(211)  评论(0编辑  收藏  举报