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

.NET MAUI:安卓端扫描带回车的条码时Entry Completed事件未触发

.NET MAUI安卓端Entry不触发Completed事件的解决方案

问题原因

Windows平台下,Entry的Completed事件会响应输入的回车符;但安卓平台上,该事件默认绑定的是软键盘的「完成」等IME操作,扫描器输入的回车符仅作为文本内容被添加,不会触发IME的完成动作,因此Completed事件无法正常触发。

解决方案

方案1:自定义Entry安卓渲染器,手动触发事件

通过自定义Entry控件并编写安卓平台渲染器,拦截回车符输入,手动触发Completed事件。

  1. 创建自定义Entry类(共享项目中):
namespace YourApp.Controls;

public class BarcodeEntry : Entry
{
}
  1. 编写安卓平台渲染器(安卓项目Platforms/Android目录下):
using Android.Content;
using Android.Widget;
using YourApp.Controls;
using YourApp.Platforms.Android;
using Microsoft.Maui.Controls.Platform;

[assembly: Microsoft.Maui.Controls.Compatibility.RendererView(typeof(BarcodeEntry), typeof(BarcodeEntryRenderer))]
namespace YourApp.Platforms.Android;

public class BarcodeEntryRenderer : EntryRenderer
{
    public BarcodeEntryRenderer(Context context) : base(context)
    {
    }

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

        if (Control is EditText editText)
        {
            // 监听文本变化,检测回车/换行符
            editText.TextChanged += (sender, args) =>
            {
                if (editText.Text.Contains("\r") || editText.Text.Contains("\n"))
                {
                    // 清理文本中的回车/换行
                    var cleanText = editText.Text.Replace("\r", "").Replace("\n", "");
                    editText.Text = cleanText;
                    // 手动触发Completed事件
                    (Element as BarcodeEntry)?.Completed?.Invoke(Element, EventArgs.Empty);
                    // 移除焦点,模拟原生Completed行为
                    editText.ClearFocus();
                }
            };
        }
    }
}
  1. 在XAML中使用自定义控件:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:YourApp.Controls"
             x:Class="YourApp.MainPage">
    <controls:BarcodeEntry x:Name="barcodeEntry" Placeholder="Scan barcode here" Completed="OnBarcodeCompleted" />
</ContentPage>

方案2:监听TextChanged事件处理回车符

无需自定义控件,直接通过TextChanged事件检测回车符并处理逻辑。

  1. 修改XAML绑定TextChanged事件:
<Entry x:Name="barcodeEntry" Placeholder="Scan barcode here" TextChanged="BarcodeEntry_TextChanged" />
  1. 后台实现事件处理:
private void BarcodeEntry_TextChanged(object sender, TextChangedEventArgs e)
{
    if (sender is Entry entry && !string.IsNullOrEmpty(entry.Text))
    {
        if (entry.Text.Contains("\r") || entry.Text.Contains("\n"))
        {
            // 提取并清理条码内容
            var barcode = entry.Text.Replace("\r", "").Replace("\n", "");
            // 清空输入框,方便下一次扫描
            entry.Text = string.Empty;
            // 执行条码处理逻辑
            HandleBarcode(barcode);
        }
    }
}

private void HandleBarcode(string barcode)
{
    // 原OnBarcodeCompleted方法中的处理逻辑
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 19:23:21