后退按钮按下事件

后退按钮按下事件

本文介绍了后退按钮按下事件 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"不是异步的我不能写 await 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 上的确认对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:09