0

Regarding this question: Is it possible to force a page constructor call on windows phone?

Background:

I am writing a Windows Phone application with DirectX using SharpDX.Toolkit. When navigating back from another page, some re-initialization has to be done. In the provided samples, this is done using the constructor, which works. However, if I place the initialization code into the page loaded event (since the constructor on my page is not called), the initialization does no longer work.

This re-initalization is not needed (and, in fact, introduces bugs into the application), when the navigation is a back navigation from another application or the navigation is induced by a fast app switch. Therefore, overloading the NavigatedTo method is not sufficient.

Community
  • 1
  • 1
Emiswelt
  • 3,909
  • 1
  • 38
  • 56

1 Answers1

1

If you want some code to be launched every time user navigates to your page, just override the OnNavigatedTo or OnNavigatedFrom method:

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    // this method is called on each navigation to the page
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    base.OnNavigatingFrom(e);
    // this method is called on each navigation from the page
}
Martin Suchan
  • 10,600
  • 3
  • 36
  • 66
  • Thank you very much for your suggestion. I probably should have been more clear in the question. I expanded my post about why the OnNavigatedTo method is not sufficient. – Emiswelt Nov 10 '13 at 20:21
  • In that case you can just check what kind of navigation is happening using the e.NavigationMode property. You can detect in those two methods navigation from/to page in your app or from/to another app or Start screen. – Martin Suchan Nov 10 '13 at 22:04
  • 1
    Thank you, detecting this special case using e.NavigationMode == NavigationMode.Back and e.IsNavigationInitiator == true worked. – Emiswelt Nov 12 '13 at 12:45