WPF故事板没有开始

WPF故事板没有开始

本文介绍了WPF故事板没有开始,如果停止()被处理之后调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个包含图像的按钮,我想该图像旋转,同时执行一些code,然后停在该处理结束。

I have a button containing an image, and I want that image rotates while some code is executed, then stop at the end of this processing.

我的code几乎工作,如果我开始故事板,但当我添加行sb.Stop(),动画从来没有开始。

My code almost works if I begin the storyboard, but when I add the line sb.Stop(), the animation never begins.

下面是我的code:

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();
    }

我卡住了。

推荐答案

我有我的解决方案。

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);
    }

整个例如code:

whole example code:

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线程做你的工作,它无法更新的用户界面,虽然不会动画旋转,当它与你的工作完成,启动和停止间动画,最后会有时间更新UI,你可以调用停止动画。这导致不显示任何动画。你需要把你的工作在不同的线程,以prevent这种情况的发生。

Ok, I want to explain what I think is your problem here. When you use the UI-Thread to do your "work" it can't update your UI and though won't animate the rotating, when its done with your "work", between starting and stopping the animation, and finally would have time to update the UI, you call to stop animating. That leads to not showing any animation. You need to put your work in a different thread to prevent this from happening.

希望有所帮助。

这篇关于WPF故事板没有开始,如果停止()被处理之后调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:32