如何等待一个RoutedEvent ?



如何等待RoutedEvent?因为没有GetAwaiter的定义,所以不能使用await关键字。那么我应该如何修改代码,使其等待GIF动画的完成?

调用routeevent的代码:

public static async Task<Boolean> Draw(List<Point> points, int counter, Polyline current, Color color, MainWindow main)
{
current.Stroke = new SolidColorBrush(color);
current.Points.Clear();
for (int i = 0; i < points.Count; i++)
{
await Task.Delay(TimeSpan.FromMilliseconds(1));
current.Points.Add(points[i]);
if(i == points.Count - 1)
{
var completed = new XamlAnimatedGif.AnimationCompletedEventArgs(current);
await ShowExplosion(main, current.Points.ElementAt(i));
}
}
points.Clear();
current = null;
return true;
}

和RoutedEvent:

static RoutedEvent ShowExplosion(MainWindow main, Point coordinate)
{
Image explosion = new Image();
explosion.Width = 40;
explosion.Height = 40;
Canvas.SetLeft(explosion, coordinate.X - 20);
Canvas.SetTop(explosion, coordinate.Y - 20);
//Relative URI
var resourcePath = new Uri("pack://application:,,,/Resources/GIF/Explosion2_trsp.gif");
XamlAnimatedGif.AnimationBehavior.SetSourceUri(explosion, resourcePath);
XamlAnimatedGif.AnimationBehavior.SetRepeatBehavior(explosion, new System.Windows.Media.Animation.RepeatBehavior(1));
XamlAnimatedGif.AnimationBehavior.SetAutoStart(explosion, true);
main.canvas_shots.Children.Add(explosion);
return XamlAnimatedGif.AnimationBehavior.AnimationCompletedEvent;
}

不等待事件。事件是同步的,是与异步方法不同的概念。当你等待异步方法的时候,你可以通过注册一个事件处理程序来监听事件,例如,用事件源(事件(c#编程指南),路由事件概述(WPF . net))进行回调。

除非您有对事件源的引用,否则您可以使用UIElement.AddHandler方法沿事件路由将事件处理程序附加到元素。在您的例子中,父MainWindow:

main.AddHandler(XamlAnimatedGif.AnimationBehavior.AnimationCompletedEvent, OnAnimationCompleted);

你的固定代码:

public static bool Draw(List<Point> points, int counter, Polyline current, Color color, MainWindow main)
{
current.Stroke = new SolidColorBrush(color);
current.Points.Clear();
for (int i = 0; i < points.Count; i++)
{
current.Points.Add(points[i]);
if (i == points.Count - 1)
{
var completed = new XamlAnimatedGif.AnimationCompletedEventArgs(current);
ShowExplosion(main, current.Points.ElementAt(i));
}
}
points.Clear();
current = null;
return true;
}
static void ShowExplosion(MainWindow main, Point coordinate)
{
Image explosion = new Image();
explosion.Width = 40;
explosion.Height = 40;
Canvas.SetLeft(explosion, coordinate.X - 20);
Canvas.SetTop(explosion, coordinate.Y - 20);
//Relative URI
var resourcePath = new Uri("pack://application:,,,/Resources/GIF/Explosion2_trsp.gif");
XamlAnimatedGif.AnimationBehavior.SetSourceUri(explosion, resourcePath);
XamlAnimatedGif.AnimationBehavior.SetRepeatBehavior(explosion, new System.Windows.Media.Animation.RepeatBehavior(1));
XamlAnimatedGif.AnimationBehavior.SetAutoStart(explosion, true);
main.canvas_shots.Children.Add(explosion);
// Listen to the event by registering a callback (event handler)
main.AddHandler(XamlAnimatedGif.AnimationBehavior.AnimationCompletedEvent, OnAnimationCompleted);
}
private void OnAnimationCompleted(object sender, AnimationCompletedEventArgs e)
{
var main = sender as UIElement;
main.RemoveHandler(XamlAnimatedGif.AnimationBehavior.AnimationCompletedEvent, OnAnimationCompleted);
// TODO::Do something after the animation has completed...
}

相关内容

  • 没有找到相关文章

最新更新