Forum Cross Platform with Xamarin

How to clear the navigation stack and insert a new page

Any one has any idea how to clear the navigation stack and insert a new page since it requires at least one element in the stack and if the new page is similar to the existing page it gives an exception saying "Page already present in the navigation stack" or " Page must not already have a parent". Need code of that.
Please help!!

Tagged:

Posts

  • GerryHGerryH USMember ✭✭✭

    So Clint, How to clear the navigation stack and insert a new page? I don't need a link to some general documentation, I need a specific time-saving answer.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    There is no single Clear() method. Loop through the navigation stack removing each item from the stack (or you could PopAsync() each item) until there are no more items in the stack.

    There is also no Insert() method for a Stack. Insert would be to put one thing between to existing items. If you are clearing first there is no insert possible. All you can do is Add. Most easily done by PushAsync() method in the documentation.

    If you have an actual need to Insert you would have to move items off the stack and store them in another queue so they don't Dispose() of themselves... then Add your new item... then add back the saved items. That would be insert into the the middle of the the navigation stack. But since you specifically said you wanted to Clear first, I suspect all you really want to do is Add() after you clear.

    @GerryH said:
    So Clint, How to clear the navigation stack and insert a new page? I don't need a link to some general documentation, I need a specific time-saving answer.

    I'm not providing copy/paste code. I'm more of a "teach a man to fish" kinda guy. Take a whack at the advice concept given. If you run in to trouble post your code effort and describe the problem and I'm happy to help get you on track.

  • MNadeemMNadeem Member ✭✭

    var existingPages = Navigation.NavigationStack.ToList();
    foreach(var page in existingPages)
    {
    Navigation.RemovePage(page);
    }

Sign In or Register to comment.