You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 14下.NET 8 MAUI Blazor升级后输入框无法唤起键盘求助

MAUI Blazor Android 升级.NET 8后InputText无法唤起软键盘问题处理

我将MAUI Blazor Android应用从.NET 7升级至.NET 8以适配最新API级别后,点击InputText输入框无法唤起软键盘,相关日志如下:

[IMM_LC] ssi() - flag : 0 view : com.companyname.appname reason = SHOW_SOFT_INPUT
[IMM_LC] ssi() view is not EditText
[IMM_LC] ssi() - cancel : servedView != view, servedView=DecorView@87f8a2e[MainActivity],focus=false,windowFocus=true,autofillUiShowing=false,window=android.view.ViewRootImpl$W@bfccff0,displayId=0,temporaryDetach=false,hasImeFocus=true, view=crc64d693e2d9159537db.BlazorAndroidWebView{86ce70d VF.DHVC.. ........ 0,0-1080,2205},focus=false,windowFocus=true,autofillUiShowing=false,window=android.view.ViewRootImpl$W@bfccff0,displayId=0,temporaryDetach=false,hasImeFocus=true

疑问解答

A) 为何标准版本升级会引发该问题?

.NET 8对MAUI Blazor的Android WebView底层实现做了调整,包括焦点管理和IME(输入法)交互逻辑的优化。升级后WebView内部的焦点传递机制发生变化,InputText触发软键盘请求时,系统识别到的焦点视图是BlazorAndroidWebView而非标准EditText,且存在服务视图(servedView)与当前视图不匹配的情况,最终导致软键盘唤起被取消。

B) 是否有其他用户遇到过此问题?

是的,已有不少MAUI Blazor用户在升级.NET 8后反馈类似软键盘唤起失败的问题,集中在Android平台WebView输入控件场景,属于.NET 8 MAUI的特定兼容性问题,在官方仓库和社区讨论中已有相关议题。

C) 有无全局修复方案?若没有,单个修复方法也可。

全局修复方案

通过自定义WebView处理程序强制修正焦点和IME交互逻辑:

  1. 在Android项目中创建自定义BlazorWebViewHandler类:
using Microsoft.Maui.Handlers;
using Android.Views.InputMethods;
using Android.Webkit;

namespace YourAppNamespace.Platforms.Android;

public class CustomBlazorWebViewHandler : BlazorWebViewHandler
{
    protected override void ConnectHandler(global::Android.Webkit.WebView platformView)
    {
        base.ConnectHandler(platformView);
        platformView.Focusable = true;
        platformView.FocusableInTouchMode = true;
        platformView.SetOnFocusChangeListener(new FocusChangeListener(platformView));
    }
}

public class FocusChangeListener : Java.Lang.Object, Android.Views.View.IOnFocusChangeListener
{
    private readonly global::Android.Webkit.WebView _webView;

    public FocusChangeListener(global::Android.Webkit.WebView webView)
    {
        _webView = webView;
    }

    public void OnFocusChange(Android.Views.View? v, bool hasFocus)
    {
        if (hasFocus)
        {
            var imm = (InputMethodManager)_webView.Context.GetSystemService(Android.Content.Context.InputMethodService);
            imm.ShowSoftInput(_webView, ShowFlags.Forced);
        }
    }
}
  1. 在MauiProgram.cs中注册自定义处理程序:
builder.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
    handlers.AddHandler<Microsoft.Maui.Controls.BlazorWebView, YourAppNamespace.Platforms.Android.CustomBlazorWebViewHandler>();
#endif
});

单个输入框修复方案

针对单个InputText添加焦点事件处理,手动触发软键盘:

<InputText @ref="inputRef" @onfocus="OnInputFocus" />

@code {
    private InputText? inputRef;

    private async void OnInputFocus(FocusEventArgs e)
    {
#if ANDROID
        var handlerFactory = MauiProgram.Current.Services.GetService<IHandlerFactory>();
        var webView = inputRef?.Parent as Microsoft.Maui.Controls.BlazorWebView;
        if (handlerFactory != null && webView != null)
        {
            var webViewHandler = (BlazorWebViewHandler)handlerFactory.GetHandler(webView);
            if (webViewHandler.PlatformView is Android.Webkit.WebView androidWebView)
            {
                var imm = (InputMethodManager)androidWebView.Context.GetSystemService(Android.Content.Context.InputMethodService);
                imm.ShowSoftInput(androidWebView, ShowFlags.Forced);
            }
        }
#endif
    }
}

内容的提问来源于stack exchange,提问作者cai120

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 20:36:12