I have four pages page 1-page 2-page 3-page 4.I use push modal async for navigating forward. When I tap button click in page 4 it is navigating to the page2. But tapping back bu
Please use await Navigation.PopModalAsync()
and read through this link to get info about ModalPages
.
EDIT:
Haven't tested it, but could help to understand the approach:
for (var i = Navigation.NavigationStack.Count - 1; i > 0; i--)
{
if (Navigation.NavigationStack[i] is Page2)
{
return;
}
await Navigation.PopModalAsync();
}
You can use the following helper functions
/// <summary>
/// Class with extension methods for INavigation interface to help working with the modal page stack
/// </summary>
public static class ModalHelper
{
/// <summary>
/// Unwinds the modal stack of the navigation until reaching a page with the given type
/// </summary>
/// <typeparam name="PageType"></typeparam>
/// <param name="navigation">The navigation object of the current top page</param>
/// <returns></returns>
public async static Task UnwindModalStackTo<PageType>(this INavigation navigation) where PageType : Page
{
await navigation.UnwindModalStackTo(p => p is PageType);
}
/// <summary>
/// Unwinds the modal stack of the navigation until reaching the given page
/// </summary>
/// <param name="navigation">The navigation object of the current top page</param>
/// <param name="page">The page where to stop unwinding the modal stack</param>
public async static Task UnwindModalStackTo(this INavigation navigation, Page page)
{
await navigation.UnwindModalStackTo(p => p == page);
}
/// <summary>
/// Unwinds the modal stack of the navigation until reaching a page that fulfils the predicate
/// </summary>
/// <param name="navigation">The navigation object of the current top page</param>
/// <param name="predicate">A function which tests whether to stop at a given page</param>
public async static Task UnwindModalStackTo(this INavigation navigation, Func<Page, bool> predicate)
{
bool found = false;
while (navigation != null && navigation.ModalStack.Count > 0)
{
// Get the current top page of the modal stack
Page topPage = navigation.ModalStack[navigation.ModalStack.Count - 1];
// Get the second page in the modal stack from the top (This one will become top next after we pop)
Page parentPage;
if (navigation.ModalStack.Count > 1)
{
parentPage = navigation.ModalStack[navigation.ModalStack.Count - 2];
}
else
{
parentPage = null;
}
// When the top page fulfills the predicate, stop
if (predicate(topPage))
{
found = true;
break;
}
// Pop the top page
await navigation.PopModalAsync();
// We need to use the navigation of the new top page from here on
navigation = parentPage?.Navigation;
}
// When the target page was not found, throw an exception
if (!found)
{
throw new Exception("Desired page not found in modal stack");
}
}
}
Note that after we have popped the top modal page from the stack, we have to use the Navigation from the new top page to continue on.
Example:
Imagine we have an app with five pages: MainPage, Page1, Page2, Page3, Page4.
Mainpage has a button to open Page1 modal, Page1 has a button to open Page2 modal, and so on.
In MainPage:
private async void Button_Clicked(object sender, EventArgs e)
{
Page p = new Page1();
await Navigation.PushModalAsync(p);
}
In Page1:
private async void Button_Clicked(object sender, EventArgs e)
{
Page p = new Page2();
await Navigation.PushModalAsync(p);
}
And so on...
Now in Page4 instead (in order to go back to Page2) we use the following code to close all open modal pages until we reach Page2.
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.UnwindModalStackTo<Page2>();
}
RemovePage
only supports removing of specified page from NavigationStack
.
Like this: Navigation.RemovePage(Navigation.NavigationStack[Navigation.NavigationStack.Count - 1]);
You can refer this for more detail: https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.inavigation?view=xamarin-forms
If you want to go 2 pages back this will work
Navigation.RemovePage(Navigation.NavigationStack[Navigation.NavigationStack.Count - 2]);
Navigation.RemovePage(this);
First we remove the page before the current page and then the current page.