Xamarin - Cannot use PopModalAsync

As it seems you had the wrong Navigation object. In my case I also had this wrong.

I displayed a modal page, which had it's own navigation bar (or Navigation object). So when I wanted to dismiss this modal page I got the mentioned exception, because there were no other pages on the navigation stack. It was the wrong object ...


Finally, I may get the answer that App.Current.MainPage.Navigation.PopModalAsync(); can do the trick. The reason is that the new LoginPage() is called as a new Content Page not existing page.

If I call it from the App.Current.MainPage (The existing LoginPage), it can get the existing modal from Modal Stack.

So the solution can be :

    public partial class LoginPage : ContentPage
    {

        public LoginPage()
        {
            InitializeComponent();

        }


        async void LoginBtnClicked(object sender, EventArgs args)
        {
            await Navigation.PushModalAsync(new AuthenicationBrowser());
        }

        public async void PopModal()
        {

            Debug.WriteLine("Navigation.ModalStack  PopModal ===> {0}", App.Current.MainPage.Navigation.ModalStack.Count);
            await App.Current.MainPage.Navigation.PopModalAsync();

        }



    }