Phoenix
Phoenix

Reputation: 467

How to pop then immediately push xamarin pages

my current navigation stack has two pages. First is page A, second is Page B. When I click my button, It'll add a new page to the navigation stack, Page C.

May I ask how do I display Page C and remove Page B, or remove page B and display page C.

I've tried the following

await Navigation.PopAsync()
await Navigation.PushAsync(new CustomPage())

The issue i'm having with this is that the page pops successfully, but the new page isn't visible. I immediately see page A. May I ask how do I pop the current page and immediately show another.

Upvotes: 11

Views: 3109

Answers (2)

TosT
TosT

Reputation: 43

You can also Remove every Page from the NavigationStack in the OnAppearing Method from the next Page. In this example the first Page...

protected override void OnAppearing()
    {
        // Remove LoginPage from NavigationStack
        if (Navigation.NavigationStack.Count > 1)
        {
            Page page = Navigation.NavigationStack.First();
            if (page != null && page != this)
            {
                Navigation.RemovePage(page);
            }
        }
        base.OnAppearing();
    }

Upvotes: 0

Nick Peppers
Nick Peppers

Reputation: 3251

Instead of popping the page and then trying to push the new page. I would push your new page then remove the previous page from the NavigationStack.

var previousPage = Navigation.NavigationStack.LastOrDefault();
await Navigation.PushAsync(new CustomPage());
Navigation.RemovePage(previousPage);

Upvotes: 21

Related Questions