WPF - 同步动画

7

我有一个这样的东西:

scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, shrinkAnimation);
scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, shrinkAnimation);
MyDialog.Show();

动画并行运行得很正常(x和y同时缩小),但由于BeginAnimation是异步调用,所以当动画仍在运行时(假设shrinkAnimation运行1秒钟)Show()方法就会被执行。

我该如何等待动画完成后再调用Show()方法呢?

谢谢!


一个比Mike更简单的解决方案是用以下代码替换我的最后一行: shrinkAnimation.Completed += delegate { MyDialog.Show(); }; - Gus Cavalcanti
1个回答

4
您可以使用具有完成事件的Storyboard来替代BeginAnimation方法。以下是一个设置不透明度的示例,但概念相同:
DoubleAnimation animation = new DoubleAnimation(0.0, new Duration(TimeSpan.FromSeconds(1.0)));

Storyboard board = new Storyboard();
board.Children.Add(animation);

Storyboard.SetTarget(animation, MyButton);
Storyboard.SetTargetProperty(animation, new PropertyPath("(Opacity)"));

board.Completed += delegate
{
    MessageBox.Show("DONE!");
};

board.Begin();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接