Go to a new page without putting the current page on the back stack?

In a Windows Phone 7 application, I have CurrentPage, which on a special event navigates to a new page using NavigationService:

NavigationService.Navigate(new Uri("/NewPage.xaml", UriKind.Relative)); 

Now that the user clicks on NewPage, I want the application to skip CurrentPage and go directly to the MainPage of the application.

I tried using NavigationService.RemoveBackEntry, but this will remove MainPage instead of CurrentPage.

How to go to a new page without putting current on the back stack?

+6
source share
3 answers

When switching to NewPage.xaml, go through the parameter so that you know when to remove the previous page from the back.

You can do it as such:

When switching from CurrentPage.xaml to NewPage.xaml, execute the parameter

     bool remove = true;
     String removeParam = remove?  bool.TrueString: bool.FalseString;

     NavigationService.Navigate (new Uri ("/ NewPage.xaml? RemovePrevious =" + removeParam, UriKind.Relative));

In the OnNavigatedTo NewPage.xaml event, check whether to delete the previous page or not.

     bool remove = false;

     if (NavigationContext.QueryString.ContainsKey ("removePrevious"))
     {
         remove = ((string) NavigationContext.QueryString ["removePrevious"]). Equals (bool.TrueString);
         NavigationContext.QueryString.Remove ("removePrevious");
     }

     if (remove)
     {
         NavigationService.RemoveBackEntry ();
     }

So you can select CurrentPage.xaml if you want to remove it from backstack.

+11
source

Where do you call "NavigationService.RemoveBackEntry ()"? I think you need to do this on a new page, not on the page you want to skip!

edit: So, to get a better picture: you have the main page → 1st subpage (should be skipped during reverse navigation) → 2nd subpage which does not depend on the 1st subpage.

2 ideas: 1) Try calling "NavigationService.RemoveBackEntry ()" in the OnNavigatedFrom event of the 1st subpage 2) Check the OnNavigatedTo-Event of the 1st subpage if NavigationMode (see Events args) == Back and move back again.

+1
source

It looks like you are calling RemoveBackEntry earlier (while you are still on CurrentPage.xaml). That is why its removal of MainPage.xaml. When you go to NewPage.xaml, in the call OnNavigatedTo NavigationService.RemoveBackEntry and this should fix the problem.

0
source

Source: https://habr.com/ru/post/917798/


All Articles