如何禁用WPF动画

时间:2012-08-01 13:49:04

标签: c# wpf

我有这段代码,无法弄清楚如何禁用动画。

有任何线索吗?谢谢!

      #region Blinking Animation
            Storyboard sb = new Storyboard();
            if (IsImageBlinking)
            {
                DoubleAnimation da = new DoubleAnimation();

                da.From = 1.0;
                da.To = 0.0;
                da.RepeatBehavior = RepeatBehavior.Forever;
                da.AutoReverse = true;


                sb.Children.Add(da);
                Storyboard.SetTargetProperty(da, new PropertyPath("(Image.Opacity)"));
                Storyboard.SetTarget(da, image1);
                sb.Begin();
            }
            else // This code doesn't disable the animation :(
            {
//!!!! Here I need to disable the animation.  
                sb.Stop();
                sb.Children.Clear();
            }

            #endregion

2 个答案:

答案 0 :(得分:5)

如果您对BeginStop使用相同的 Storyboard实例,则此方法有效。将sb声明为您班级的成员:

public class MainWindow
{
    private Storyboard sb = new Storyboard();

    ...
}

答案 1 :(得分:2)

您是否尝试过使用sb.BeginAnimation(Image.OpacityProperty, null);代替sb.stop()