Share via

Remove Current Page

jrahma 116 Reputation points
2021-04-06T20:49:38.63+00:00

Hi,

I am opening a ModalPage called MyPage22 from a page called MyPage11 this way:

await App.Current.MainPage.Navigation.PushModalAsync(new Xamarin.Forms.NavigationPage(new PlaceDetails(place_id)));

How can I remove MyPage11 before opening the MyPage22?

I tried:

async void Button_Clicked(System.Object sender, System.EventArgs e)
{
    Navigation.RemovePage(this);
    await App.Current.MainPage.Navigation.PushModalAsync(new Xamarin.Forms.NavigationPage(new MyPage22()));
}

but getting:

System.InvalidOperationException: Cannot remove root page when it is also the currently displayed page.

Developer technologies | .NET | Xamarin
Developer technologies | C#
Developer technologies | 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.

0 comments No comments

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,971 Reputation points
    2021-04-07T02:45:57.52+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    System.InvalidOperationException: Cannot remove root page when it is also the currently displayed page

    The page cannot be removed when it's currently displayed page. You could remove the 'MyPage11' in the OnDisAppearing() method of the 'MyPage22'. Or set the 'MyPage22' as the root page directly, which will remove the 'MyPage11' in the navigation stack.

       async void Button_Clicked(System.Object sender, System.EventArgs e)  
        {  
            App.Current.MainPage = new MyPage22();  
        }  
    

    Best Regards,

    Jarvan 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.

    Was this answer helpful?


Your answer

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