1

I'm having trouble with my WP8 app. It uses the MVVM light library for the creation of the ViewModel and is also used for the page navigation with Messages.

Right now I'm having some problems with the OnNavigatedTo Method of my MainPage. If I start the app for the first time, it is called once. If I close and reopen it with the back button, it is also only called once. The constructor of my MainPage ViewModel will also be called only once.

If I let the application open in the background and exit with the Start or Search button, OnNavigatedTo will be called twice! The constructor will not be called any more.

My problem is now, that I'm initialising in the OnNavigatedTo a synchronsiation with a server. This should only be done once, but now it is sometimes called twice because of the double call of OnNavigatedTo. My understanding of the WP8 app model was that OnNavigatedTo will be only called once when I enter the page. I cannot understand this behaviour and it is driving me nuts.

I'm not using the FastAppResume and just want the normal behaviour back. Any advice would be really appreciated! I've checked all calls where I could have possibly loaded the page twice, but this did not help. Besides this problem, my app behaves like it should. I'm otherwise looking for a solution to create an async task which can be executed only one at a time.

Markus Rudel
  • 1,318
  • 15
  • 30
  • Have you checked the callstacks on each callback to see what code is triggering the double callback? As a hacky workaround you could set an "initialized" member variable to avoid the double work. – Oren Jun 03 '13 at 15:54
  • What is the value of NavigationMode for each call? – trydis Jun 03 '13 at 22:42
  • yes I've checked the callstack but it's only calling regular Windows Phone code before it access my onNavigatedTo, nothing from my code. If I start a new debug session and leave the app with the home button and enter it from a tile on the startscreen, I get the following navigationModes: new, (1st call) reset, (2nd call) refresh. My understanding was that onNavigatedTo should always be called only once?! – Markus Rudel Jun 04 '13 at 07:16
  • Oren: I've tried the hacky workaround. At least it would call the code only once, but then I had some other issues with sometimes not registeres message handler. – Markus Rudel Jun 05 '13 at 09:38

1 Answers1

0

I've had this happen before, it's because the Message was being registered multiple times.

Without seeing your code te best way to fix this is to add the Messenger.Default.Register to your ViewModel constructor so it's only called once

Bugail
  • 46
  • 2
  • yes that could be a problem. I register and unregister the Messenger, because they will be added more than once or react on pages to delayed messages. But I need to register some of them in the MainPage.xaml.cs because registering them in the ViewModel would be to late :/ I need this e.g. for the page navigation I realized with Messages. But I will look into this, good to hear that I'm not alone with this strange issue! – Markus Rudel Jun 04 '13 at 10:10
  • It's OK to register in the MainPage, but make sure that you unregister in the page's OnNavigatingFrom. – LBugnion Jun 05 '13 at 08:41
  • Ok thats good to hear. I register and deregister the Messenger when I enter or leave the page. Can Messenger collide with other Messenger registered on other pages? e.g. for page navigation? – Markus Rudel Jun 05 '13 at 09:40