Search code examples
c#windows-phone-8

How to reload a Windows phone application page without creating a new copy in the memory?


In my Windows phone application, I'm trying to reload the application page by using the following code:

NavigationService.Navigate(new Uri(string.Format("/Page1.xaml?random={0}", Guid.NewGuid()), UriKind.Relative));

I have written the code above to the button click event. The Page1 is reloading fine but every time when I click the button the app memory keep on increasing and at some time app is crashed.

Is there any other way to reload or refresh the page with out creating a new copy of the page in the app memory.


Solution

  • You can always refresh the content of the page from code by calling Page.Refresh(); or some similar method, but this may not refresh all the content.

    If you decide to do a new navigation (and ensure that everything gets to the initial state of the page), you can remove the previously navigated pages from the stack by calling:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
       if (NavigationContext.QueryString.ContainsKey("logedin"))
       {
           NavigationService.RemoveBackEntry();
       }
    }
    

    Take a look at the NavigationService class, specially the AddBackEntry and RemoveBackEntry methods:

    http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice(v=vs.110).aspx