Confirmation dialog on the back button, click the Xamarin.Forms event

I am stuck at one point in the Xamarin.Forms application

Press the "Back" button, just ask the user to confirm whether he really wants to exit or not, "OnBackButtonPressed" I want to show the Alert dialog and continue as the user's response.

But "OnBackButtonPressed" is not Async, I cannot write waitAlert ... Please call me, how can I implement this function?

I use ContentPage as my NavigationPage root page

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 } } 
+9
source share
5 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; } 
+30
source
 public override void OnBackPressed() { RunOnUiThread( async () => { var isCloseApp = await AlertAsync(this, "NameOfApp", "Do you want to close this app?", "Yes", "No"); if (isCloseApp) { var activity = (Activity)Forms.Context; activity.FinishAffinity(); } }); } public Task<bool> AlertAsync(Context context, string title, string message, string positiveButton, string negativeButton) { var tcs = new TaskCompletionSource<bool>(); using (var db = new AlertDialog.Builder(context)) { db.SetTitle(title); db.SetMessage(message); db.SetPositiveButton(positiveButton, (sender, args) => { tcs.TrySetResult(true); }); db.SetNegativeButton(negativeButton, (sender, args) => { tcs.TrySetResult(false); }); db.Show(); } return tcs.Task; } 

Xamarin.Android are waiting for AlertDialog.Builder

+1
source

An easy way to override the hardware back button and show a confirmation dialog to the user

 protected override bool OnBackButtonPressed() { Device.BeginInvokeOnMainThread(async () => { if (await DisplayAlert("Alert", "Are you sure you want to go back ?", "Yes", "No")) { base.OnBackButtonPressed(); await Navigation.PopAsync(); } }); return true; } 
0
source

If you are on the main page and want to exit the application, this code will help you.

In your interface (PCL)

  protected override bool OnBackButtonPressed() { Device.BeginInvokeOnMainThread(new Action(async () => { var result = await this.DisplayAlert("Alert!", "Do you really want to exit?", "Yes", "No"); if (result) { if (Device.RuntimePlatform == Device.Android) DependencyService.Get<IAndroidMethods>().CloseApp(); } })); return true; } 

Also create an interface (in your PCL interface):

 public interface IAndroidMethods { void CloseApp(); } 

Now embed Android-specific logic in your Android project:

 [assembly: Xamarin.Forms.Dependency(typeof(AndroidMethods))] namespace Your.Namespace { public class AndroidMethods : IAndroidMethods { public void CloseApp() { var activity = (Activity)Forms.Context; activity.FinishAffinity(); } } } 
0
source
  private async void OnDelete(object sender, EventArgs e) { var result = await this.DisplayAlert("Alert!", "Do you really want to exit?", "Yes", "No"); if (result) { var menuitem = sender as MenuItem; string name = menuitem.BindingContext as string; lielements.Remove(name); } else { } } 
-one
source

All Articles