Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - synchronous animation

I have something this:

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

The animations run correctly in parallel (x and y shrink together), but because BeginAnimation is an asynchronous call, the Show() method gets executed while the animation is still running (suppose shrinkAnimation runs for 1 second).

How can I wait for animations to complete before calling Show()?

Thanks!

like image 388
Gus Cavalcanti Avatar asked Feb 04 '10 20:02

Gus Cavalcanti


1 Answers

You can use a Storyboard, which has a completed event, instead of that BeginAnimation method. Here's an example, setting opacity, but it's the same concept:

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();
like image 90
Mike Pateras Avatar answered Oct 20 '22 22:10

Mike Pateras