WPF - synchronous animation
-
18-09-2019 - |
Question
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!
Solution
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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow