5

I feel silly, but I just can't find an answer to this. Let's say I want to show some message to the users when the MainPage loads. The problem is that if I do this on Loaded event (MessageBox.Show()), the popup shows just before my page is loaded which leaves the user with this message and a black background. This is kind of dull situation. Any ideas which event can do the trick. There other ways around like background worker or NavigationInTransition_EndTransition event but there should be more elegant way ?

Miro
  • 442
  • 6
  • 14
  • Not really sure if this works on WP7, but give it a try: `Dispatcher.BeginInvoke(new Action(delegate() { MessageBox.Show("Hello"); }));` – NestorArturo Jun 28 '11 at 18:31

5 Answers5

7

You can easily test all standard methods. It took me less time than writing this text to do so.

In page constructor:

Loaded += new RoutedEventHandler(TestPage_Loaded);
LayoutUpdated += new EventHandler(TestPage_LayoutUpdated);

Respective methods:

    void TestPage_LayoutUpdated(object sender, EventArgs e)
    {
        Debug.WriteLine("LayoutUpdated");
    }

    void TestPage_Loaded(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("Loaded");
        Dispatcher.BeginInvoke(() =>
        {
            Debug.WriteLine("Loaded -> BeginInvoke");
        });
    }

Add debug write to OnNavigateTo() as well.

If your page is similarly complex than mine, then you get something like:

  1. OnNavigatedTo
  2. LayoutUpdated
  3. Loaded
  4. LayoutUpdated
  5. Loaded -> BeginInvoke
  6. many times LayoutUpdated

Last LayoutUpdated would be the best candidate, but how would one detect it? Hence "Loaded -> BeginInvoke" seems to be the best option among the trivial ones.

You can also use the Loaded events of individual components on your page. That's also trivially easy. They will probably happen sometimes between 4th and 5th step.

If none of above options helps (I don't think so), you have to use non-standard logic. Such as above mentioned one-shot timer that shows the message after some time. Or base your logic on LayoutUpdated counter.

Jan Slodicka
  • 1,505
  • 1
  • 11
  • 14
1

If Loaded fires to early for you, you could try LayoutUpdated. This event is fired each time a layout pass occurs, and typically will fire after Loaded. However, you should remember to unsubscribe to this event when no longer needed. I use this event in the DeferredLoadContentControl which I created:

http://www.scottlogic.co.uk/blog/colin/2011/01/windows-phone-7-deferredloadcontentcontrol/

ColinE
  • 68,894
  • 15
  • 164
  • 232
  • Thanks for the advice. It will do the work. Actually this is pretty much the case with NavigationInTransition_EndTransition which produces pretty much the same result I guess. Still, I wondered why there is not a single page event that would fit the task. – Miro Jun 28 '11 at 20:58
  • LayoutUpdated fires many times depending on the layout complexity. It often fires first time before the Loaded event. – Jan Slodicka Jun 29 '11 at 08:37
  • True, my standard approach to using LayoutUpdated is to each time it fires, check whether the element I need to locate is present in the visual tree. When I can find the control I want I unsubscribe. – ColinE Jun 29 '11 at 08:56
1

You can use a DispatcherTimer in the LoadedEvent to fire after a second or so.

1

Try Page.OnNavigatedTo instead of Loaded.

Shawn Wildermuth
  • 7,318
  • 3
  • 23
  • 28
  • Just tried overriding OnNavigatedTo out of curiosity, it still happens before the page loads, so you get the same effect as showing the message box during Loaded. – E.Z. Hart Jun 28 '11 at 22:59
  • 1
    Shawn, OnNavigatedTo is fired before Loaded and I need the last possible event. – Miro Jun 29 '11 at 13:31
  • Wasn't sure, but it was worth a try...Honestly I probably wouldn't use a MessageBox, but instead show a Popup so the order didn't matter. But that's me. – Shawn Wildermuth Jul 01 '11 at 07:27
0

You could also attach it to the Loaded event for one of your controls.

I'm not sure what you're wanting to have in the background (the entire page?), but adding this to whatever control you want to show up might work.

ginman
  • 1,315
  • 10
  • 20