English 中文(简体)
Override/Disable Android Back Button in Blazor Hybrid MAUI
原标题:

I have a .razor page which takes a lot of time to load. Is it possible to disable/override the hardware Back button in Blazor Hybrid MAUI for a specific .razor page? The primary reason for doing so is to make sure the user doesn t accidentally presses it and have to wait for the loading again. We are working on a more sophisticated cache to handle this better but for now we just want to override the back button to switch tab inside of the page rather than full page back.

问题回答

You can do that by setting a keylistener for the webview.

  1. Custom a keylistener in the android part:
namespace BlazorApp.Platforms.Android
{
    public class MyListener : Java.Lang.Object, IOnKeyListener
    {
        public bool OnKey(global::Android.Views.View v, [GeneratedEnum] Keycode keyCode, KeyEvent e)
        {
            if(keyCode == Keycode.Back && e.Action == KeyEventActions.Down)
            {
                return true; // this will cancel the back button pressed event
            }
            return false;
        }
    }
}
  1. And in the MainPage.xaml:
<BlazorWebView x:Name="webview" HostPage="wwwroot/index.html">
        <BlazorWebView.RootComponents>
            <RootComponent Selector="#app" ComponentType="{x:Type local:Main}" />
        </BlazorWebView.RootComponents>
    </BlazorWebView>
  1. Override the OnHandlerChanged() method in the MainPage.cs:
protected override void OnHandlerChanged()
    {
        base.OnHandlerChanged();
#if ANDROID
        var web = webview.Handler.PlatformView as Android.Webkit.WebView;
        web.SetOnKeyListener(new Platforms.Android.MyListener());
#endif
    }

Update:

You can add a judgement before you cancel the back button event. Such as:

namespace BlazorApp.Platforms.Android
{
    public class MyListener : Java.Lang.Object, IOnKeyListener
    {
        public bool OnKey(global::Android.Views.View v, [GeneratedEnum] Keycode keyCode, KeyEvent e)
        {
            if(keyCode == Keycode.Back && e.Action == KeyEventActions.Down)
            {
                string url = (v as gloabl::Android.Webkit.WebView).Url;
                if(url.EndsWith("specialpage"));// the razor page s route is @page"/specialpage"
                   return true; // this will cancel the back button pressed event
            }
            return false;
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签