How to PopAsync more than 1 page in Xamarin Forms Navigation?

前端 未结 5 998
一生所求
一生所求 2021-02-07 04:27

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

5条回答
  •  梦毁少年i
    2021-02-07 04:31

    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();
    }
    

提交回复
热议问题