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 = "动画已完成";
        }
    }
}
相关推荐
✎ ﹏梦醒͜ღ҉繁华落℘10 小时前
WPF学习(四)
学习·wpf
zzyzxb11 小时前
WPF中依赖属性和附加属性
wpf
✎ ﹏梦醒͜ღ҉繁华落℘11 小时前
WPF学习(动画)
学习·wpf
weixin_447103581 天前
Wpf布局之Canvas面板!
wpf
葬歌倾城1 天前
waferMap图像渲染
c#·wpf
甄天1 天前
WPF路由事件:冒泡、隧道与直接全解析
c#·wpf·visual studio
三千道应用题1 天前
WPF学习笔记(12)下拉框控件ComboBox与数据模板
wpf
界面开发小八哥1 天前
界面组件DevExpress WPF中文教程:Grid - 如何获取节点?
.net·wpf·界面控件·devexpress·ui开发
三千道应用题1 天前
WPF学习笔记(22)项面板模板ltemsPanelTemplate与三种模板总结
wpf
佛·追命2 天前
.net wpf混淆
.net·wpf