Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirmation Dialog on back button press event Xamarin.Forms

Tags:

I am stuck at one point in Xamarin.Forms application

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.

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

I am using ContentPage as my root Page of NavigationPage

    public class frmHome : ContentPage

Here is the code :

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
        }
}
like image 290
Afzal Ali Avatar asked Aug 23 '15 05:08

Afzal Ali


1 Answers

    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;
    }
like image 169
Daniel Luberda Avatar answered Sep 20 '22 13:09

Daniel Luberda