In my app I\'m pushing pages using a NavigationPage
and at some stage I want to go back to a previous page in the stack. This is my structure:
NavigationPag
I came with a solution which can go back to an specific page as long as you have a reference to it:
protected async Task PopToPage(Page destination)
{
if (destination == null) return;
//First, we get the navigation stack as a list
var pages = Navigation.NavigationStack.ToList();
//Then we invert it because it's from first to last and we need in the inverse order
pages.Reverse();
//Then we discard the current page
pages.RemoveAt(0);
foreach (var page in pages)
{
if (page == destination) break; //We found it.
toRemove.Add(page);
}
foreach (var rvPage in toRemove)
{
navigation.RemovePage(rvPage);
}
await Navigation.PopAsync();
}