0

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

如果我开始故事板,我的代码几乎可以工作,但是当我添加 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();
    }

我被困住了。

4

1 回答 1

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,请调用停止动画。这导致不显示任何动画。您需要将您的工作放在不同的线程中以防止这种情况发生。

希望有帮助。

于 2012-05-30T14:39:12.313 回答