Unable to Remove previous page

Jassim Al Rahma 1,551 Reputation points
2021-10-03T22:20:18.397+00:00

Hi,

I am trying to remove the previous page using below code:

var previousPage = Navigation.NavigationStack.OfType<NewPost>()?.FirstOrDefault();

Navigation.RemovePage(previousPage);

but it is not closing the previous page nor showing an error.

Kindly help..

Thanks,
Jassim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,634 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 29,301 Reputation points Microsoft Vendor
    2021-10-04T02:20:01.017+00:00

    Hello,
    Welcome to our Microsoft Q&A platform!

    The index of the previousPage in the NavigationStack is not FirstOrDefault(), stack follows the LIFO (Last In First Out) principle. So, its index is NavigationStack.Count - 2. You could also use the name of page to find which page you want to remove, refer to the following code:

    private void Button_Clicked(object sender, EventArgs e)  
            {  
             //var previousPage = Navigation.NavigationStack.OfType<FirstPage>()?.FirstOrDefault();  
            //Navigation.RemovePage(previousPage);  
            //this.Navigation.PopAsync();  
    
                this.Navigation.RemovePage(this.Navigation.NavigationStack[this.Navigation.NavigationStack.Count - 2]);  
                this.Navigation.PopAsync();  
            }  
    

    In fact, FirstPage has been removed, however, we need pop the current page to check the effect.
    Best Regards,
    Wenyan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.