如果在处理后调用Stop(),则WPF StoryBoard不会启动

时间:2012-05-30 10:30:18

标签: c# wpf animation storyboard

我有一个包含图像的按钮,我想在执行某些代码时旋转图像,然后在此处理结束时停止。

如果我开始使用故事板,我的代码几乎可以正常工作,但是当我添加sb.Stop()行时,动画永远不会开始。

这是我的代码:

private void refreshPostIt(int postItIndex)
    {
        Button btn = // Button to rotate

        Storyboard sb = new Storyboard();
        DoubleAnimation rotate = new DoubleAnimation();
        rotate.From = 0;
        rotate.To = 360;
        rotate.RepeatBehavior = RepeatBehavior.Forever;

        RotateTransform rt = new RotateTransform();
        btn.RenderTransformOrigin = new Point(0.5, 0.5);
        btn.RenderTransform = rt;
        Storyboard.SetTarget(rotate, btn);
        Storyboard.SetTargetName(rotate, btn.Name);
        Storyboard.SetTargetProperty(rotate, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
        sb.Children.Add(rotate);
        sb.Begin(this, true);

        // Some code which can take several seconds

        sb.Stop();
    }

我被困住了。

1 个答案:

答案 0 :(得分:0)

我有一个适合我的解决方案。

private void refreshPostIt()
    {
        // Button btn; is defined somewhere else

        Storyboard sb = new Storyboard();
        DoubleAnimation rotate = new DoubleAnimation();
        rotate.From = 0;
        rotate.To = 360;
        rotate.RepeatBehavior = RepeatBehavior.Forever;

        RotateTransform rt = new RotateTransform();
        btn.RenderTransformOrigin = new Point(0.5, 0.5);
        btn.RenderTransform = rt;
        Storyboard.SetTarget(rotate, btn);
        Storyboard.SetTargetName(rotate, btn.Name);
        Storyboard.SetTargetProperty(rotate, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
        sb.Children.Add(rotate);
        sb.Begin(btn, true);

            // Do your stuff here (Not in the UI Thread)
            // Maybe use a semaphore to lock the sb.Stop(btn)

        sb.Stop(btn);
    }

整个示例代码:

public partial class Window1 : Window
{
    Button btn;
    Storyboard sb;
    public Window1()
    {
        btn = new Button();
        InitializeComponent();
        this.grid.Children.Add(btn);
        refreshPostIt();
    }

    private void refreshPostIt()
    {
        // Button btn; is defined somewhere else

        sb = new Storyboard();
        DoubleAnimation rotate = new DoubleAnimation();
        rotate.From = 0;
        rotate.To = 360;
        rotate.RepeatBehavior = RepeatBehavior.Forever;

        RotateTransform rt = new RotateTransform();
        btn.RenderTransformOrigin = new Point(0.5, 0.5);
        btn.RenderTransform = rt;
        Storyboard.SetTarget(rotate, btn);
        Storyboard.SetTargetName(rotate, btn.Name);
        Storyboard.SetTargetProperty(rotate, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
        sb.Children.Add(rotate);
        sb.Begin(btn, true);

        BackgroundWorker asd = new BackgroundWorker();
        asd.DoWork += new DoWorkEventHandler(asd_DoWork);
        asd.RunWorkerCompleted += new RunWorkerCompletedEventHandler(asd_RunWorkerCompleted);
        asd.RunWorkerAsync();

    }

    void asd_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (sb != null)
        {
            sb.Stop(btn);
        }
    }

    void asd_DoWork(object sender, DoWorkEventArgs e)
    {
        Thread.Sleep(2000);
    }
}

好的,我想在这里解释一下我认为你的问题。当你使用UI-Thread进行“工作”时,它无法更新你的UI,虽然它不能动画旋转,当它完成你的“工作”,开始和停止动画之间,最后会有时间要更新UI,您可以调用停止动画。这导致不显示任何动画。您需要将您的工作放在不同的线程中以防止这种情况发生。

希望有所帮助。