So I created a PCL app with 3 pages: page 1, page 2 and page 3. Each page has Title Page 1, Page 2 or Page 3, and I put one button on each page. Then I write code as below to jump from page 1 to page 2 and then from page 2 to page 3 when clicking on the button.
On page 1 button click event handler:
await Navigation.PushAsync(new Page2());
await Navigation.PushAsync(new Page3());
Navigation.RemovePage(Navigation.NavigationStack.ElementAt(1));
await Navigation.PopAsync();
If you are only using the PushAsync
to push pages into the navigation stack and there is no modals, tabbed pages, master detail page etc, then the best solution that is directly supported by Xamarin.Forms would be to use await Navigation.PopToRootAsync()
. It would pop all the modals from your navigation stack except the Root. But it takes into consideration only the top most modal stack.
If in case your navigation stack is complex with modals, master detail, etc then I would suggest you to do it directly using the MainPage
property than going the route you are trying to do (ie: Playing with Navigation service).
At first set the MainPage
in App.cs to Page1
MainPage = new Navigationpage(Page1());
Do the same as you do in the events till you show up the Page3
In the Page3
button event do the following :
MainPage = new Navigationpage(Page1());
This would reset your Application's MainPage
, both your Page3
and Page2
would be gone of the Navigation stack.
Drawback : The state of controls of Page1 wont be preserved, you will have to store it in some variables in the OnDisappearing
event.