WPF基础入门-Class9-动画基础

WPF基础入门

Class9-动画基础

好了,后续可以开始尝试winform转wpf,开始用wpf做项目了
参考:WPF项目实战合集(2022终结版)
1、创建一个Button

<Grid>
        <Button x:Name="btn"  Width="100" Height="40" Click="Test_Click">Button>
Grid>

2、Test_Click

private void Test_Click(object sender, RoutedEventArgs e)
        {
            //创建双精度的动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = btn.Width; //动画初始值
            animation.To = btn.Width - 30; //动画的结束值 or animation.By = -30;

            animation.Duration = TimeSpan.FromSeconds(0.5); //动画持续时间
            //animation.AutoReverse = true; //执行结束后自动恢复 
            //animation.RepeatBehavior = new RepeatBehavior(3); //动画重复执行次数 重复前会回复初始状态 RepeatBehavior.Forever无限

            animation.Completed += Animation_Completed;

            //启动动画
            btn.BeginAnimation(Button.WidthProperty, animation);
        }

        private void Animation_Completed(object sender, EventArgs e)
        {
            btn.Content = "Completed";
        }

执行结果:
Button按钮动态变化

你可能感兴趣的:(WPF基础入门,wpf)