上善若水

水善利万物而不争
随笔 - 175, 文章 - 0, 评论 - 10, 阅读 - 14万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

WPF | 跟着视频敲代码12 | 动画基础Animation

Posted on   董锡振  阅读(60)  评论(0编辑  收藏  举报

https://www.bilibili.com/video/BV1nY411a7T8?p=13

 

 

 

复制代码
<Window x:Class="WPF04.Animation"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF04"
        mc:Ignorable="d"
        Title="Animation" Height="280" Width="600">
    <Grid>
        <Button x:Name="btn" Content="执行动画"  Width="100" Height="30" Margin="5" Click="Button_Click"></Button>
    </Grid>
</Window>
复制代码
复制代码
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

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

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //创建一个双精度的动画
            DoubleAnimation anmation = new DoubleAnimation();
            anmation.By = -30;
            //anmation.From = btn.Width;  //设置动画的开始值
            //anmation.To = btn.Width - 30;//设置动画的结束值
            anmation.Duration = TimeSpan.FromSeconds(1);//持续时间 秒
            anmation.AutoReverse = true; //自动返回
            anmation.RepeatBehavior = new RepeatBehavior(2); //循环2次
            anmation.Completed += Anmation_Completed;        //动画完成的事件
            //在当前按钮上执行该动画
            btn.BeginAnimation(Button.WidthProperty, anmation);
        }

        private void Anmation_Completed(object? sender, EventArgs e)
        {
            btn.Content = "动画已完成";
        }
    }
}
复制代码

 

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示