后退按钮按下事件Xamarin.Forms上的“确认对话框" [英] Confirmation Dialog on back button press event Xamarin.Forms

查看:856
本文介绍了后退按钮按下事件Xamarin.Forms上的“确认对话框"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在Xamarin.Forms应用程序中的某个地方

I am stuck at one point in Xamarin.Forms application

在返回"按钮上按一下,我只是想让用户确认他是否真的要退出, "OnBackButtonPressed"我想显示警报"对话框并作为用户响应继续进行.

On Back button press simply I want to ask user to confirm whether he really wants to exit or not, "OnBackButtonPressed" I want to show Alert dialog and proceed as user response.

但是"OnBackButtonPressed"不是异步我无法编写等待DisplayAlert ... 请指导我如何实现此功能?

But "OnBackButtonPressed" is not Async I cannot write await DisplayAlert... Please direct me how should i implement this feature?

我正在使用ContentPage作为NavigationPage的根页面

I am using ContentPage as my root Page of NavigationPage

    public class frmHome : ContentPage

这是代码:

protected override bool OnBackButtonPressed()
{
    var result = await  this.DisplayAlert("Alert!", "Do you really want to exit?", "Yes", "No");

    if (result)
        {
            //user wants to exit
            //Terminate application
        }
        else
        {
            //Dont do anything
        }
}

推荐答案

    protected override bool OnBackButtonPressed()
    {
        Device.BeginInvokeOnMainThread(async() => {
            var result = await this.DisplayAlert("Alert!", "Do you really want to exit?", "Yes", "No");
            if (result) await this.Navigation.PopAsync(); // or anything else
        });

        return true;
    }

这篇关于后退按钮按下事件Xamarin.Forms上的“确认对话框"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆