Unable to Remove previous page

Jassim Al Rahma 1,616 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

Developer technologies | .NET | Xamarin
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.