WPF中动画

动画命名空间:using System.Windows.Media.Animation;

<Grid>

<Button

Content="执行动画"

x:Name="btn"

Width="100"

Height="40"

Click="btn_Click"></Button>

</Grid>

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            //创建一个双精度的动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = btn.Width;//动画的初始值
            animation.To = btn.Width - 30; //动画的结束值
            animation.Duration = TimeSpan.FromSeconds(2);//动画的持续时间
            animation.AutoReverse = true;//动画变化后恢复原状
            animation.RepeatBehavior = new RepeatBehavior(5);//new 执行次数 //RepeatBehavior.Forever;//一直重复执行   执行周期

            animation.Completed += animation_Completed;//动画完成后执行的事件


            btn.BeginAnimation(Button.WidthProperty, animation);
        }
        private void animation_Completed(object sender, EventArgs e)
        {
            btn.Content = "动画已完成";
        }
    }
}
相关推荐
Pota-to成长日记9 分钟前
Redisson 看门狗机制深度解析:分布式锁的守护者
分布式·wpf
为java加瓦41 分钟前
分布式单例模式在微服务架构中的关键作用与实践
wpf
c#上位机2 小时前
wpf之Ellipse控件
wpf
c#上位机2 小时前
wpf之GroupBox
c#·wpf
分布式存储与RustFS11 小时前
告别手动配置:用 Terraform 定义你的 RustFS 存储帝国
云原生·wpf·文件系统·terraform·对象存储·minio·rustfs
c#上位机1 天前
wpf之TabControl
c#·wpf
mingupup1 天前
WPF应用最小化到系统托盘
wpf
qiangshang9901262 天前
WPF+MVVM入门学习
学习·wpf
DASXSDW2 天前
Abp vNext-事件总线使用实现及解析
ui·wpf
纸照片2 天前
【邪修玩法】如何在WPF中开放 RESTful API 服务
后端·wpf·restful