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

MAUI安卓应用中条码扫描器无法触发Entry的Completed事件

解决MAUI安卓外接Datalogic扫描器触发Entry.Completed事件的问题

针对你遇到的默认键盘下Entry.Completed事件不触发的问题,提供以下几种可行方案:

1. 改用KeyPress事件+超时判断触发解密逻辑

由于默认键盘对Completed事件的触发逻辑与第三方键盘存在差异,且扫描器未输出换行符,可通过监听按键输入并判断输入间隔来识别扫描完成时机:

private DateTime _lastKeyPressTime;
private readonly TimeSpan _inputTimeout = TimeSpan.FromMilliseconds(500);
private CancellationTokenSource _cts;

private void Entry_KeyPressed(object sender, KeyEventArgs e)
{
    _lastKeyPressTime = DateTime.Now;
    _cts?.Cancel();
    _cts = new CancellationTokenSource();

    Task.Run(async () =>
    {
        try
        {
            await Task.Delay(_inputTimeout, _cts.Token);
            if (DateTime.Now - _lastKeyPressTime >= _inputTimeout)
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    // 执行条码解密逻辑
                    DecodeBarcode(((Entry)sender).Text);
                });
            }
        }
        catch (TaskCanceledException)
        {
            // 输入未中断,忽略
        }
    });
}

将Entry的KeyPressed事件绑定到上述方法,当扫描器连续输入结束后,超时触发解密操作。

2. 配置Datalogic扫描器添加后缀换行符

扫描器在电脑端正常工作可能是因为驱动自动添加了换行后缀,安卓端可通过Datalogic官方配置应用(如Datalogic Scanner Settings),设置扫描器在条码末尾输出\n或\r字符。添加后缀后,默认键盘下Entry的Completed事件会在接收到换行时正常触发。

3. 安卓平台自定义渲染器强制触发Completed事件

通过MAUI自定义渲染器直接在安卓原生层捕获外接键盘的Enter事件,手动触发MAUI Entry的Completed事件:

[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace YourAppNamespace.Platforms.Android
{
    public class CustomEntryRenderer : EntryRenderer
    {
        public CustomEntryRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.KeyPress += Control_KeyPress;
                Control.SetOnEditorActionListener(new EditorActionListener(Element));
            }
        }

        private void Control_KeyPress(object sender, Android.Views.View.KeyEventArgs e)
        {
            if (e.KeyCode == Android.Views.Keycode.Enter)
            {
                e.Handled = true;
                ((IEntryController)Element)?.SendCompleted();
            }
        }

        private class EditorActionListener : Java.Lang.Object, Android.Widget.IOnEditorActionListener
        {
            private readonly Entry _entry;

            public EditorActionListener(Entry entry) => _entry = entry;

            public bool OnEditorAction(Android.Widget.TextView v, int actionId, Android.Views.KeyEvent e)
            {
                if (actionId == Android.Views.InputMethods.ImeAction.Done || 
                    (e != null && e.KeyCode == Android.Views.Keycode.Enter && e.Action == Android.Views.KeyEventActions.Down))
                {
                    ((IEntryController)_entry)?.SendCompleted();
                    return true;
                }
                return false;
            }
        }
    }
}

该渲染器会监听原生EditText的Enter按键和IME动作,主动触发MAUI层面的Completed事件,绕过默认键盘的事件拦截逻辑。

4. 禁用输入法干扰特性

在Entry控件中禁用拼写检查和文本预测功能,减少默认键盘对外接扫描器事件的干扰:

<Entry IsSpellCheckEnabled="False" IsTextPredictionEnabled="False" />

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:42:04