using System.Windows; using System.Windows.Media.Animation; namespace _15_动画基础_1修改按钮动画_P354; /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void ButtonAnimation_OnClick(object sender, RoutedEventArgs e) { var doubleAnimation = new DoubleAnimation { // 开始值 From = 160, // 结束值 To = Width - 30, // 增大10个单位,不使用To而是使用By // By = 10, // 翻转动画 AutoReverse = true, // 比原来快5倍 SpeedRatio = 5, // 持续2秒 Duration = TimeSpan.FromSeconds(2), // 一直循环 // RepeatBehavior = RepeatBehavior.Forever // 循环四次 // RepeatBehavior = new RepeatBehavior(4) // 循环3秒 RepeatBehavior = new RepeatBehavior(TimeSpan.FromSeconds(3)) }; ButtonAnimation.BeginAnimation(WidthProperty, doubleAnimation); } private void ButtonAnimation2_OnClick(object sender, RoutedEventArgs e) { var doubleAnimation = new DoubleAnimation { From = ButtonAnimation.ActualWidth, To = Width - 30, Duration = TimeSpan.FromSeconds(2) }; ButtonAnimation.BeginAnimation(WidthProperty, doubleAnimation); } }