Spongebob Comrade
Spongebob Comrade

Reputation: 1568

Is there any event after Loaded and before showing the user control?

I'm loading user controls in a specific grid in our application and I'm adding animations to the loading user controls in the Loaded event handler of the controls.

Some of the user controls are lightweight and some of them need heavy computations before loading. The problem is that when I add a 1 second animation to the heavy user controls then showing the control so endures that the animation never can be seen!

Is there any other event that I can add my animation to the user control so that the heavy ones and the light ones get displayed with the same animation?

I need an event right before showing the control. OnLoaded doesn't seem to be right place for me, unfortunately.

Upvotes: 6

Views: 10510

Answers (3)

Martin Braun
Martin Braun

Reputation: 12579

For those who are looking for a generic way to execute code right after components being created but before they are rendered the LayoutUpdated event helps. This should work inside of windows as well as inside of user or custom controls.

Upvotes: 0

Spongebob Comrade
Spongebob Comrade

Reputation: 1568

The answer was giving the Storyboard.Begin a lower priority.

I found out it from this answer : https://stackoverflow.com/a/4708172/970420

So we have this code for beginning the animation :

    void BeginStoryboardAction(Storyboard sb)
    {
        sb.Begin();
    }

and we should call it by this way :

    Dispatcher.BeginInvoke(new Action<Storyboard>(BeginStoryboardAction), DispatcherPriority.ContextIdle, sb);

sb is an instance of Storyboard which has some animations in it.

[Edit] : Another shorter way :

Dispatcher.BeginInvoke(new Action<Storyboard>(delegate (Storyboard stb){stb.Begin();}), DispatcherPriority.ContextIdle, sb);

Upvotes: 5

AkselK
AkselK

Reputation: 2593

When a window is first opened, the Loaded and ContentRendered events are raised only after the Activated event is raised. With this in mind, a window can effectively be considered opened when ContentRendered is raised.

http://msdn.microsoft.com/en-us/library/ms748948.aspx

Upvotes: 3

Related Questions