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 = "动画已完成";
        }
    }
}
相关推荐
dotent·10 小时前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
咩图2 天前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
雁于飞2 天前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native
oioihoii2 天前
WPF入门指南:解析默认项目结构
wpf
极客智造2 天前
深入解析 ReactiveUI:WPF 响应式 MVVM 开发的 “终极方案”
wpf
Macbethad3 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
lingxiao168884 天前
WPF Prism框架应用
c#·wpf·prism
Macbethad4 天前
使用WPF编写一个Ethercat主站的程序
wpf
难搞靓仔4 天前
WPF 弹出窗体Popup
wpf·popup
Macbethad4 天前
使用WPF编写一个MODBUSTCP通信的程序
wpf