Search code examples
c#xamlwindows-phone-8sharpdx

Force a page constructor call on windows phone on back navigation


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.


Solution

  • 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
    }