4

So I have a Storyboard that will run multiple animations one after each other (not all of them simultaneously) and in some of those animations I subscribe to the DoubleAnimation.Completed event. The animations run perfectly fine, but the Completed event of the animations are only triggered once the whole Storyboard is finished, not when the individual animation is finished (BeginTime of next animation). For example:

Storyboard storyboard = new Storyboard();

DoubleAnimation hideAnimation = new DoubleAnimation();
hideAnimation.Duration = TimeSpan.FromSeconds(0.5);
hideAnimation.BeginTime = TimeSpan.FromSeconds(0);
hideAnimation.From = 1.0;
hideAnimation.To = 0.0;
hideAnimation.Completed += new EventHandler(HideAnimation_Completed);
Storyboard.SetTarget(hideAnimation, grid1);
Storyboard.SetTargetProperty(hideAnimation, new PropertyPath(Grid.OpacityProperty));

DoubleAnimation showAnimation = new DoubleAnimation();
showAnimation.Duration = TimeSpan.FromSeconds(0.5);
showAnimation.BeginTime = TimeSpan.FromSeconds(0.5);
showAnimation.From = 1.0;
showAnimation.To = 0.0;
showAnimation.Completed += new EventHandler(ShowAnimation_Completed);
Storyboard.SetTarget(showAnimation, grid2);
Storyboard.SetTargetProperty(showAnimation, new PropertyPath(Grid.OpacityProperty));

storyboard.Children.Add(hideAnimation);
storyboard.Children.Add(showAnimation);
storyboard.Begin();

In this example HideAnimation_Completed will be called after 1 second (total duration of storyboard) and after HideAnimation_Completed is done, ShowAnimation_Completed is called.

Is was expecting HideAnimation_Completed to be called after 0.5 seconds and ShowAnimation_Completed after 1 second, not both after 1 second.

Those anyone knows if this an intended behaviour of WPF or am I missing something?

NOTE: The previous code is just an example to explain the problem I have. In my real program I have multiple animations that are dynamically generated and added into a global Storyboard at different parts of the the class. So the idea of starting each animation at the end of the previous one using the Completed event and UIElement.BeginAnimation method, is the last of my resources since that would involve modifying quite a lot of my code (and it would probably have a great effect over the performance of my application).

Andrea Antonangeli
  • 1,242
  • 1
  • 21
  • 32
Agustin0987
  • 581
  • 6
  • 15
  • How does it behave if you run the animations independently, without a Storyboard? E.g. `grid1.BeginAnimation(Grid.OpacityProperty, hideAnimation);` – Clemens Sep 19 '16 at 08:40
  • I can't really test individual animations since (as I mentioned in the question) they are dynamically generated on run time with information that comes from different parts of the program. That code in the question is not actual code of my program is just a sample that I type just to help me explain what the problem is (maybe adding that code was a bad idea). – Agustin0987 Sep 19 '16 at 08:45
  • You can still try if the example you've posted behaves differently if you call BeginAnimation independently, just to get an idea of a possible solution or workaround. – Clemens Sep 19 '16 at 08:51
  • You are right. If I try I can actually verify if this is an intended behavior or not. Let me try and I let you know how it goes. – Agustin0987 Sep 19 '16 at 08:53
  • Yes indeed it seems to be an intended behavior in WPF. I just tried the code in a separate application and I still have the same problem. – Agustin0987 Sep 19 '16 at 08:56

0 Answers0