MAUI Blazor學習16-連續按BACK退出APP
MAUI Blazor系列目錄
- MAUI Blazor學習1-移動客戶端Shell佈局 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習2-建立移動客戶端Razor頁面 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習3-繪製ECharts圖表 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習4-繪製BootstrapBlazor.Chart圖表 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習5-BLE低功耗藍芽 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習6-掃描二維碼 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習7-實現登入跳轉頁面 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習8-支援多語言 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習9-VS Code開發除錯MAUI入門 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習10-BarcodeScanner掃描二維碼 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習11-百度地圖定位 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習12-檔案另存為 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習13-開啟檔案 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習14-選擇目錄 - SunnyTrudeau - 部落格園 (cnblogs.com)
- MAUI Blazor學習15-採用html2pdf.js生成pdf - SunnyTrudeau - 部落格園 (cnblogs.com)
安卓APP一般都是連續按2次BACK鍵退出,MAUI Blazor可以判斷當前導航頁面如果為最後一個,就啟動退出檢測。網上有現成的例程,照抄即可。參考https://blog.csdn.net/sD7O95O/article/details/132632457,《MAUI APP 釋出指南(三)vivo篇-CSDN部落格.mhtml》
在Platforms\Android\MainActivity.cs過載按鍵事件。
D:\Software\gitee\mauiblazorapp\MaBlaApp\Platforms\Android\MainActivity.cs
public class MainActivity : MauiAppCompatActivity { private bool isExit = false; public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent? e) { //最後一個頁面按BACK才觸發 System.Diagnostics.Debug.WriteLine($"呼叫OnKeyDown, keyCode={keyCode} keyEvent={e}"); if (keyCode == Keycode.Back) { var modelStack = Microsoft.Maui.Controls.Application.Current.MainPage.Navigation.ModalStack; if (modelStack.Count == 0) { if (isExit) { //第2次返回鍵 Microsoft.Maui.Controls.Application.Current.Quit(); } else { //第一次返回鍵 isExit = true; Task.Run(async () => { await Task.Delay(2000); isExit = false; }); //用安卓的方法也可以 //new Android.OS.Handler().PostDelayed(() => //{ // isExit = false; //}, 2000); Toast.MakeText(this, "再按一次退出", ToastLength.Long).Show(); return true; } } } return base.OnKeyDown(keyCode, e); } public override bool DispatchKeyEvent(KeyEvent? e) { //每次按BACK都觸發 System.Diagnostics.Debug.WriteLine($"呼叫DispatchKeyEvent, keyEvent={e}"); return base.DispatchKeyEvent(e); } }
測試的時候發現點選BACK回退頁面時,觸發了2次DispatchKeyEvent,按下和彈起,但是沒有觸發OnKeyDown,直到退回到最後一頁,才會觸發OnKeyDown。
【正常回退頁面】
[0:] 呼叫DispatchKeyEvent, keyEvent=KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=95052961, downTime=95052961, deviceId=-1, source=0x101, displayId=0 }
[0:] 呼叫DispatchKeyEvent, keyEvent=KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=95053063, downTime=95052961, deviceId=-1, source=0x101, displayId=0 }
【回退到最後一個頁面】
[0:] 呼叫DispatchKeyEvent, keyEvent=KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=95068879, downTime=95068879, deviceId=-1, source=0x101, displayId=0 }
[0:] 呼叫OnKeyDown, keyCode=Back keyEvent=KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=95068879, downTime=95068879, deviceId=-1, source=0x101, displayId=0 }
[0:] 呼叫DispatchKeyEvent, keyEvent=KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=95069038, downTime=95068879, deviceId=-1, source=0x101, displayId=0 }
【回退到最後一個頁面,快速2次BACK退出APP】
[0:] 呼叫DispatchKeyEvent, keyEvent=KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=95094450, downTime=95094450, deviceId=-1, source=0x101, displayId=0 }
[0:] 呼叫OnKeyDown, keyCode=Back keyEvent=KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=95094450, downTime=95094450, deviceId=-1, source=0x101, displayId=0 }
[0:] 呼叫DispatchKeyEvent, keyEvent=KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=95094575, downTime=95094450, deviceId=-1, source=0x101, displayId=0 }
[0:] 呼叫DispatchKeyEvent, keyEvent=KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=95095075, downTime=95095075, deviceId=-1, source=0x101, displayId=0 }
[0:] 呼叫OnKeyDown, keyCode=Back keyEvent=KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=95095075, downTime=95095075, deviceId=-1, source=0x101, displayId=0 }
DEMO程式碼地址:https://gitee.com/woodsun/mauiblazorapp