none
WPF动画停止不了... RRS feed

  • 问题

  • public partial class Window1 : Window
      {
        private Storyboard myStoryboard;
        private DoubleAnimation myDoubleAnimation;
    
        public Window1()
        {
          InitializeComponent();
    
          myDoubleAnimation = new DoubleAnimation();
          myDoubleAnimation.Name = "ABC";
          myDoubleAnimation.From = 0;
          myDoubleAnimation.To = 359;
          myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
          myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
          myStoryboard = new Storyboard();
          myStoryboard.Children.Add(myDoubleAnimation);
         
          RotateTransform rotateTransform = new RotateTransform(0);
          rotateTransform.CenterX = 50;
          rotateTransform.CenterY = 50;
          MyButton.RenderTransform = rotateTransform;
          Storyboard.SetTargetName(MyButton, "ABC");
          Storyboard.SetTargetProperty(myDoubleAnimation,
            new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
    
          myStoryboard.Begin(MyButton);
        }
    
        private void MyButton_Click(object sender, RoutedEventArgs e)
        {
          myStoryboard.Stop();
          myStoryboard.Stop(MyButton);
        }
      }
    如上述示例代码,我想做到点击Mybutton时停掉这个旋转动画,但无论是storyboard.stop()还是.stop(MyButton)都无法停止这段动画,不知道是什么原因?或者有没有其他的方法能停止这个动画?先谢谢了~

    2011年5月23日 2:17

答案

  • 这样做:

    private Storyboard myStoryboard;

    private DoubleAnimation myDoubleAnimation;

    public MainWindow()

    {

        InitializeComponent();

        MyButton.Click += delegate(object sender, RoutedEventArgs args)

        {

            myStoryboard.Stop(MyButton);

        };

        myDoubleAnimation = new DoubleAnimation();

        myDoubleAnimation.Name = "ABC";

        myDoubleAnimation.From = 0;

        myDoubleAnimation.To = 359;

        myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));

        myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;

        myStoryboard = new Storyboard();

        myStoryboard.Children.Add(myDoubleAnimation);

     

        RotateTransform rotateTransform = new RotateTransform(0);

        rotateTransform.CenterX = 50;

        rotateTransform.CenterY = 50;

        MyButton.RenderTransform = rotateTransform;

        Storyboard.SetTargetName(MyButton, "ABC");

        Storyboard.SetTargetProperty(myDoubleAnimation,

            new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));

     

        myStoryboard.Begin(MyButton, true);

     

    }

     

     

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 Colinfc 2011年5月24日 9:38
    2011年5月24日 9:12
    版主

全部回复

  • 这样做:

    private Storyboard myStoryboard;

    private DoubleAnimation myDoubleAnimation;

    public MainWindow()

    {

        InitializeComponent();

        MyButton.Click += delegate(object sender, RoutedEventArgs args)

        {

            myStoryboard.Stop(MyButton);

        };

        myDoubleAnimation = new DoubleAnimation();

        myDoubleAnimation.Name = "ABC";

        myDoubleAnimation.From = 0;

        myDoubleAnimation.To = 359;

        myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));

        myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;

        myStoryboard = new Storyboard();

        myStoryboard.Children.Add(myDoubleAnimation);

     

        RotateTransform rotateTransform = new RotateTransform(0);

        rotateTransform.CenterX = 50;

        rotateTransform.CenterY = 50;

        MyButton.RenderTransform = rotateTransform;

        Storyboard.SetTargetName(MyButton, "ABC");

        Storyboard.SetTargetProperty(myDoubleAnimation,

            new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));

     

        myStoryboard.Begin(MyButton, true);

     

    }

     

     

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 Colinfc 2011年5月24日 9:38
    2011年5月24日 9:12
    版主
  • 我也遇到这个问题了,因为界面及动画都是美工做好的,很复杂,要改成后台动画挺麻烦的,我想解决的是“后台如何停止前台定义的动画”
    2014年4月24日 2:59