WPF:后完成动画返回一个方法 [英] WPF: Returning a method AFTER an animation has completed

查看:214
本文介绍了WPF:后完成动画返回一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参照<一个href=\"http://stackoverflow.com/questions/505040/developing-a-robo$c$c-type-game-with-net-for-a-school-assignment\">this编程比赛我目前正在建设。

With reference to this programming game I am currently building.

我有一个类库(DLL),将有一个这将是由像这样的方法运行

I have a Class Library (dll) that will have a method Run which will be composed of something like such:

public class MyRobot : Robot 
{
    public void Run(} 
    {
        while (true) 
        {
           Ahead(200); //moves the bot 200pixels
           TurnLeft(90); //turns the bot by 90deg 
        }
    }
}

在这些方法(从机器人继承),系统将动画使用WPF机器人(使用 BeginAnimation 或在 DispatcherTimer )。

In those methods (inherited from Robot), the system will animate the robot using WPF (using BeginAnimation or the DispatcherTimer).

现在的问题是,我不知道的方法完成当前的前返回(即移动到下一个方法),因为这将导致动画无限循环发生一起,而当(像上面的),这是特别不好的。

Now, the problem is that I don't a method to return (ie, move on to the next method) before completing the current one, because that will result in the animations taking place together, and when in an infinite loop (like the one above), that's especially not good.


我的问题是,什么是prevent的最佳方式无法完成动画之前返回的方法

我现在有一个布尔机器人类( isActionRunning ),它被标记为真正时的动作开始运行,然后在一个动画回调转变为(使用已完成事件,如果使用 BeginAnimation )。

I currently have a bool in the Robot class (isActionRunning) that be flagged to true when an action starts running and then changes to false in an animation callback (using the Completed event if using BeginAnimation).

在每个方法的末尾,我把下面的循环(调用 BeginAnimation 之后):

At the end of each method (after invoking BeginAnimation) I placed the following loop :

while (isActionRunning) 
{
    Thread.Sleep(200); //so that the thread sleeps for 200ms and then checks again if the animation is still running 
}

这是使该方法不会在动画完成前返回。

This is so that the method won't return before the animation finishes.

不过,我觉得这是不这样做的正确方法。

But I feel that this is not the right way to do this.

任何人都可以指导我什么是最好的,以实现这一目标?

Can anyone guide me to what's best to achieve this ?

推荐答案

下面是一个选项,如果Robot.Run code是在不同的线程比UI线程做动画运行它才会工作。

Here's one option, it will only work if the Robot.Run code is running in a different thread than the UI thread doing the animations.

在Robot.Ahead方法(例如),则使用Dispatcher.Invoke(未BeginInvoke的)来调用启动动画的方法中,比在机器人用空块添加一个锁(锁(本){})

In the Robot.Ahead method (for example), use Dispatcher.Invoke (not BeginInvoke) to call the method that starts the animation, than add a lock on the robot with an empty block( lock(this) { } ).

在开始动画之前启动动画调用Monitor.Enter(机器人)的方法

In the method that starts the animation call Monitor.Enter(robot) before starting the animation

在动画完整的处理程序调用Monitor.Leave(机器人)

In the animation complete handler call Monitor.Leave(robot)

其结果将是

time      robot thread          UI thread  
  |       ---------------       --------------  
  |       call Invoke    --->  
  |                             lock robot (Monitor.Enter)  
  |                             begin animation  
  |       Invoke returns <---   return  
  |       lock(this)            (animation running)  
  |       (wait for lock  
  |       to become available)  
  |  
  |                             Animation complete  
  |                             release lock (Monitor.Exit)  
  |       (lock available,  
  |       continue running)  
  |       Release lock (exit lock block)  
  |       Return and start next  
  |       movement  
  \/

这篇关于WPF:后完成动画返回一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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