MAUI Blazor学习16-连续按BACK退出APP

MAUI Blazor学习16-连续按BACK退出APP

MAUI Blazor系列目录

  1. MAUI Blazor学习1-移动客户端Shell布局 - SunnyTrudeau - 博客园 (cnblogs.com)
  2. MAUI Blazor学习2-创建移动客户端Razor页面 - SunnyTrudeau - 博客园 (cnblogs.com)
  3. MAUI Blazor学习3-绘制ECharts图表 - SunnyTrudeau - 博客园 (cnblogs.com)
  4. MAUI Blazor学习4-绘制BootstrapBlazor.Chart图表 - SunnyTrudeau - 博客园 (cnblogs.com)
  5. MAUI Blazor学习5-BLE低功耗蓝牙 - SunnyTrudeau - 博客园 (cnblogs.com)
  6. MAUI Blazor学习6-扫描二维码 - SunnyTrudeau - 博客园 (cnblogs.com)
  7. MAUI Blazor学习7-实现登录跳转页面 - SunnyTrudeau - 博客园 (cnblogs.com)
  8. MAUI Blazor学习8-支持多语言 - SunnyTrudeau - 博客园 (cnblogs.com)
  9. MAUI Blazor学习9-VS Code开发调试MAUI入门 - SunnyTrudeau - 博客园 (cnblogs.com)
  10. MAUI Blazor学习10-BarcodeScanner扫描二维码 - SunnyTrudeau - 博客园 (cnblogs.com)
  11. MAUI Blazor学习11-百度地图定位 - SunnyTrudeau - 博客园 (cnblogs.com)
  12. MAUI Blazor学习12-文件另存为 - SunnyTrudeau - 博客园 (cnblogs.com)
  13. MAUI Blazor学习13-打开文件 - SunnyTrudeau - 博客园 (cnblogs.com)
  14. MAUI Blazor学习14-选择目录 - SunnyTrudeau - 博客园 (cnblogs.com)
  15. MAUI Blazor学习15-采用html2pdf.js生成pdf - SunnyTrudeau - 博客园 (cnblogs.com)

 

安卓APP一般都是连续按2BACK键退出,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回退页面时,触发了2DispatchKeyEvent,按下和弹起,但是没有触发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 }

 

【回退到最后一个页面,快速2BACK退出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

 

posted on 2024-06-15 10:53  SunnyTrudeau  阅读(44)  评论(0编辑  收藏  举报