学海无涯

导航

统计

动画 Animation

1
using System.Windows.Media.Animation;
1
2
3
4
5
<Grid>
        <StackPanel>
            <Button x:Name="btn" Content="执行动画" Width="100" Height="40" Click="Button_Click"/>
        </StackPanel>
    </Grid>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void Button_Click(object sender, RoutedEventArgs e)
      {
          //创建一个双精度的动画
          DoubleAnimation animation = new DoubleAnimation();
          animation.By = -30;//设置范围,在原来的基础上减30
          //animation.From = btn.Width;//动画开始值
          //animation.To = btn.Width - 30;//动画结束值
          animation.Duration = TimeSpan.FromSeconds(3);//动画的持续时间
          animation.AutoReverse = true;//执行完动画之后,恢复到原始状态
          //animation.RepeatBehavior = RepeatBehavior.Forever;//动画一直持续执行
          //animation.RepeatBehavior = new RepeatBehavior(3);//动画执行3次
 
          animation.Completed += Animation_Completed;
          //在当前按钮上执行该动画
          btn.BeginAnimation(Button.WidthProperty, animation);
      }
 
      private void Animation_Completed(object? sender, EventArgs e)
      {
          btn.Content = "动画已完成";
      }

  

 

posted on   宁静致远.  阅读(213)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示