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

.NET Maui iOS端数字键盘无返回键导致Completed事件无法触发求解决方案

解决.NET 7 Entry控件iOS数字键盘无返回键导致Completed事件不触发的问题

方案1:自定义Entry+iOS渲染器添加完成工具栏

iOS原生数字键盘默认不带完成键,我们可以通过自定义渲染器给Entry添加带完成按钮的辅助工具栏,点击按钮时触发Completed事件并收起键盘。

  1. 在共享项目中定义自定义Entry:
public class NumericEntryWithDone : Entry
{
    // 仅作为标记类供渲染器识别,无需额外逻辑
}
  1. 在iOS项目中添加自定义渲染器:
[assembly: ExportRenderer(typeof(NumericEntryWithDone), typeof(NumericEntryWithDoneRenderer))]
namespace YourAppNamespace.iOS.Renderers
{
    public class NumericEntryWithDoneRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control == null) return;

            // 创建工具栏
            var toolbar = new UIToolbar(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 44));
            toolbar.BarStyle = UIBarStyle.Default;

            // 添加灵活空间让完成按钮靠右对齐
            var flexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
            var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, (sender, args) =>
            {
                // 触发Entry的Completed事件
                Element?.Completed?.Invoke(Element, EventArgs.Empty);
                // 收起键盘
                Control.ResignFirstResponder();
            });

            toolbar.SetItems(new[] { flexibleSpace, doneButton }, false);
            // 给输入框绑定辅助工具栏
            Control.InputAccessoryView = toolbar;
        }
    }
}
  1. 在XAML中使用自定义Entry:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:YourAppNamespace"
             x:Class="YourAppNamespace.YourPage">
    <local:NumericEntryWithDone Keyboard="Numeric" 
                                Completed="OnNumericEntryCompleted"
                                Placeholder="请输入数字"/>
</ContentPage>

方案2:使用Behavior实现(无需子类化Entry)

如果不想创建自定义Entry,可以用Behavior给现有Entry动态添加完成工具栏,更灵活:

  1. 在共享项目中定义Behavior:
public class NumericEntryDoneBehavior : Behavior<Entry>
{
    protected override void OnAttachedTo(Entry bindable)
    {
        base.OnAttachedTo(bindable);
        bindable.Focused += OnEntryFocused;
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        base.OnDetachingFrom(bindable);
        bindable.Focused -= OnEntryFocused;
    }

    private void OnEntryFocused(object sender, FocusEventArgs e)
    {
        var entry = sender as Entry;
        if (entry == null || DeviceInfo.Platform != DevicePlatform.iOS)
            return;

        // 获取iOS原生UITextField控件
        var nativeTextField = Platform.GetRenderer(entry).NativeView as UITextField;
        if (nativeTextField == null)
            return;

        // 创建工具栏
        var toolbar = new UIToolbar(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 44));
        toolbar.BarStyle = UIBarStyle.Default;

        var flexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
        var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, (s, args) =>
        {
            entry.Completed?.Invoke(entry, EventArgs.Empty);
            nativeTextField.ResignFirstResponder();
        });

        toolbar.SetItems(new[] { flexibleSpace, doneButton }, false);
        nativeTextField.InputAccessoryView = toolbar;
    }
}
  1. 在XAML中绑定Behavior:
<Entry Keyboard="Numeric" 
       Completed="OnNumericEntryCompleted"
       Placeholder="请输入数字">
    <Entry.Behaviors>
        <local:NumericEntryDoneBehavior />
    </Entry.Behaviors>
</Entry>

方案说明

  • 两个方案本质都是给iOS数字键盘添加带完成按钮的辅助栏,点击完成按钮时会触发Completed事件,和Android端的返回键行为一致。
  • 方案1适合需要多处复用的场景,自定义Entry可以直接在各处引用;方案2更灵活,无需修改Entry类型,直接给现有Entry添加行为即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 14:38:26