Search code examples
c#wpfanimationwindoweventtrigger

Creating an animation to run when window loads


I have created an animation that takes 400 milliseconds to run, and attached it to an EventTrigger for the Loaded RoutedEvent of my Window.

But I think the window doesn't show up right away after it's loaded*, so I can't see the animation at all.

What are some common patterns for running animations when a Window loads?

And also, when should I run animations in separate threads?

* It probably draws the window after it is loaded and it shows the window after it's finished drawing, clarification would be helpful.


Solution

  • I have tried the solutions suggested in the following answer:

    https://stackoverflow.com/a/8886941/494094

    • Window.ContentRendered
    • UIElement.LayoutUpdated
    • Window.Activated

    So far, Window.Activated event seems to suit me best. The only caveat is that none of those events are RoutedEvents, so you need to define the animation in the code behind like the following:

    this.Activated += (sender, args) => ((Storyboard) FindResource("MyAnimation")).Begin();