为什么我的应用程序不要求确认退出?

Why my application is not asking for confirmation to exit?

我有一个MasterDetailPage,详情页是导航页。当详细页面堆栈 > 1 时,我需要 PopAsync。当它 = 1 时,应用程序需要询问用户是否需要。目前, 它只有在堆栈中有 2 个页面(根页面和第二个页面)时才有效,如果你有 3 个页面,它会异步弹出所有页面并转到根页面。此外,如果您已经在根页面,它不会询问,只需关闭应用程序。

PS: 确认对话框在应用程序的其他部分工作正常。

 public async override void OnBackPressed()
        {
            if (Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed))
                await App.Current.MainPage.Navigation.PopPopupAsync();
            else
            {
                if (App.Current.MainPage is MasterDetailPage mdp)
                {
                    if (mdp.Detail.Navigation.NavigationStack.Count > 1)
                        await mdp.Detail.Navigation.PopAsync();
                    else
                    {
                        Alerta alerta = new Alerta();
                        bool opt = await alerta.ShowAlert("confirm", "App name", "Não existem páginas para retornar, você já está na página inicial.", "Continuar", "Encerrar");
                        if (!opt)
                            Finish();
                    }
                }
                else
                {
                    Alerta alerta = new Alerta();
                    bool opt = await alerta.ShowAlert("confirm", "App name", "Não existem páginas para retornar, você já está na página inicial.", "Continuar", "Encerrar");
                    if (!opt)
                        Finish();
                }
            }
        }

在PCL

protected override bool OnBackButtonPressed()
        {
            base.OnBackButtonPressed();
            // Custom logic for BackButtonPresssed

                Device.BeginInvokeOnMainThread(async () =>
                {
                    var result = await DisplayAlert("Warning", "Are you sure you want to exit the application?", "Yes", "No");
                    if (result)
                    {
                        MessagingCenter.Send<HomePage>(this, "Shutdown");
                    }
                });
                return true;

        }

在Android MainActivity OnCreate()

添加以下代码

MessagingCenter.Subscribe<HomePage>(this, "Shutdown", (sender) =>
                {
                    OnShutdown();
                });