WPF:在执行操作之前等待故事板动画完成的正确方法是什么 [英] WPF: What is the correct way to wait for a storyboard animation to complete before performing an operation

查看:73
本文介绍了WPF:在执行操作之前等待故事板动画完成的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个故事板,我可以重用它来为一些图片制作动画,我想在每个动画之后执行一些操作,其中包括一些计算,然后运行另一个动画,所以我相信我应该使用 StoryBoard 的 Completed Event MyStoryboard.Completed += storyboard_Completed;

I have a storyboard which I reuse to animate some pictures, I wanna perform some operation after each animation, which includes some calculations, and then running another animation, so I believe I should be using the StoryBoard's Completed Event MyStoryboard.Completed += storyboard_Com​pleted;

我很好奇的是,我应该在当前 StoryBoard 的 Storyboard_Completed Event 中开始下一个动画吗?并且,如果我使用 Application.Current.Dispatcher 对象在单独的线程中启动第一个动画,会有什么影响吗?

What I'm curious about is, should I start the next animation in the current StoryBoard's Storyboard_Completed Event? And, are there any implications if I started the first animation in a separate thread using the Application.Current.Dispatcher Object?

如果我使用 Application.Current.Dispatcher 在单独的线程中调用 StoryBoard.Begin(),那么在 UI 线程中是否也会调用 Storyboard_Completed 事件?在这种情况下,我是否还需要将 Next Animation 包装在另一个 Dispatcher Invoke 中?

If I called a StoryBoard.Begin() in a separate thread using the Application.Current.Dispatcher, does the Storyboard_Completed Event also get invoked in the UI thread? In this case, do I still need to wrap the Next Animation within another Dispatcher Invoke?

private void Story_Completed(object sender, EventArgs e)
{
    Application.Current.Dispatcher.Invoke((Action)delegate()
    {
       SomeNewStoryBoardAnimation.Begin();
    }
}

这是正确的吗?或者有没有更好的方法来检查故事板是否已经结束并开始下一组计算&之后的故事板动画?

Is this correct? Or is there a better way to check if a storyboard has ended and start the next set of calculations & storyboard animation right after that?

我曾想过使用单个后台工作人员按顺序处理所有动画和计算,但我也想知道如何在开始下一组计算和动画之前等待"动画完成.BackGroundWorker 在等待动画完成时有 Thread.sleep 正常吗?

I've thought of using a single background worker to handler all animations and calculations in sequence, but I'm also wondering how to "wait" for the animation to complete before starting on the next set of calculations and animations. Is it normal for a BackGroundWorker to have Thread.sleep while waiting for animation to complete?

推荐答案

您可以将 Storyboard 包装在 Task 对象和 等待完成.

You could wrap the Storyboard in a Task object and await its completion.

这里是一段很好的示例代码,说明了如何做到这一点,摘自 博文,作者 Morten Nielsen:

Here is an excellent bit of sample code illustrating how to do just that, taken from a blog post by Morten Nielsen:

public static class StoryboardExtensions
{
    public static Task BeginAsync(this Storyboard storyboard)
    {
        System.Threading.Tasks.TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
        if (storyboard == null)
            tcs.SetException(new ArgumentNullException());
        else
        {
            EventHandler<object> onComplete = null;
            onComplete = (s, e) => {
                storyboard.Completed -= onComplete; 
                tcs.SetResult(true); 
            };
            storyboard.Completed += onComplete;
            storyboard.Begin();
        }
        return tcs.Task;
    }
}

本质上,您正在创建一个扩展方法,该方法返回一个 Task 对象,表示 Storyboard 的完成.通过这种方式,您可以获得一些不错的流畅语法,如下所示:

Essentially you're creating an extension method, which returns a Task object signalling the completion of the Storyboard. In this way, you get some nice fluid syntax like this:

//Start the storyboard and asynchronously await completion...
await myStoryboard.BeginAsync();

//Do my other stuff here, after the storyboard completes...

这篇关于WPF:在执行操作之前等待故事板动画完成的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆