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 = "动画已完成";
        }
    }
}
相关推荐
code bean1 小时前
【WPF】WPF 项目实战:构建一个可增删、排序的光源类型管理界面(含源码)
wpf
沉到海底去吧Go6 小时前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
lph19726 小时前
自定义事件wpf
wpf
code bean10 小时前
【WPF】从普通 ItemsControl 到支持筛选的 ItemsControl:深入掌握 CollectionViewSource 用法
wpf
碎碎念的安静21 小时前
WPF可拖拽ListView
c#·wpf
界面开发小八哥1 天前
界面组件DevExpress WPF中文教程:Grid - 如何识别行和卡片?
.net·wpf·界面控件·devexpress·ui开发
TwilightLemon3 天前
WPF 使用CompositionTarget.Rendering实现平滑流畅滚动的ScrollViewer,支持滚轮、触控板、触摸屏和笔
wpf
Vae_Mars4 天前
WPF中自定义消息弹窗
wpf
Magnum Lehar4 天前
GameEngine游戏引擎前端界面wpf页面实现
前端·游戏引擎·wpf
TA远方5 天前
【C#】一个简单的http服务器项目开发过程详解
服务器·http·c#·wpf·web·winform·console