為避免不小心按下 back 按鈕,立即離開APP,所以需要跳出「確認視窗」,確認使用者是不是真的要離開APP。
要達到此功能,只要複寫OnBackPressed涵式即可。
public override void OnBackPressed()
{
var alertDialog1 = new AlertDialog.Builder(this).Create();
alertDialog1.SetTitle("Alert!!");// 設定Title
alertDialog1.SetMessage("Do you really want to exit?"); // 內文
alertDialog1.SetIcon(Resource.Drawable.Icon);
alertDialog1.SetButton("Yes", (sender, args) => base.OnBackPressed());
alertDialog1.SetButton2("No", (sender, args) => alertDialog1.Cancel());
alertDialog1.SetCancelable(false);
alertDialog1.Show();
}