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 = "动画已完成";
        }
    }
}
相关推荐
碎碎念的安静31 分钟前
WPF 与 Qt 进程间通信(IPC)
开发语言·qt·wpf
军训猫猫头1 天前
7.带输入参数的线程启动 C# + WPF 完整示例
开发语言·前端·c#·.net·wpf
周杰伦fans2 天前
WPF Prism 框架完全入门指南:从环境搭建到弹窗导航实战
wpf
雨浓YN2 天前
WPF MVVM 模式(无调库)项目创建笔记
笔记·wpf
周杰伦fans2 天前
.NET AOT技术深度解析:为什么WPF不支持而Avalonia/UWP支持?
.net·wpf
雨浓YN2 天前
WPF MVVM 模式(调Prism库)项目创建笔记 —— 包含C++/CLI OpenCV互操作
c++·笔记·wpf
七夜zippoe2 天前
DolphinDB数据模型:表、分区与分布式表
分布式·wpf·数据模型··dolphindb
一念春风3 天前
Qwen2.5 (AI模型 PC搭建)
人工智能·ai·c#·wpf·模型
互联网散修3 天前
鸿蒙跨设备实时绘图同步:从零到一实现分布式画板
分布式·wpf·harmonyos
晓纪同学4 天前
WPF-09 命令系统
wpf