NET8 MAUI中Entry控件无法禁用拼写检查与文本预测
.NET 8 MAUI 彻底禁用Entry拼写检查和文本预测的方案
单纯设置IsSpellCheckEnabled="False"和IsTextPredictionEnabled="False"有时无法生效,因为不同平台的原生控件有各自的配置逻辑,需要针对每个平台修改原生属性,以下是两种可行方案:
方案一:全局统一配置(推荐)
在MauiProgram.cs中注册Entry的Handler映射,一次性对所有Entry生效:
using Microsoft.Maui.Handlers; var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() // 其他初始化配置... .ConfigureMauiHandlers(handlers => { EntryHandler.Mapper.AppendToMapping("DisableSpellCheckAndPrediction", (handler, view) => { #if IOS // iOS平台:关闭自动修正和拼写检查 handler.PlatformView.AutocorrectionType = UIKit.UITextAutocorrectionType.No; handler.PlatformView.SpellCheckingType = UIKit.UITextSpellCheckingType.No; #elif ANDROID // Android平台:禁用输入建议 handler.PlatformView.InputType = Android.Text.InputTypes.TextFlagNoSuggestions | Android.Text.InputTypes.ClassText; #elif WINDOWS // Windows平台:关闭拼写检查并设置输入范围为普通文本 if (handler.PlatformView is Microsoft.UI.Xaml.Controls.TextBox textBox) { Microsoft.UI.Xaml.Controls.SpellCheck.SetIsEnabled(textBox, false); textBox.InputScope = new Microsoft.UI.Xaml.Input.InputScope(); textBox.InputScope.Names.Add(new Microsoft.UI.Xaml.Input.InputScopeName(Microsoft.UI.Xaml.Input.InputScopeNameValue.Text)); } #endif }); }); return builder.Build();
方案二:单个Entry单独配置
如果只需要针对特定Entry生效,可以在页面中处理HandlerChanged事件:
XAML代码
<Entry Placeholder="测试输入框" IsSpellCheckEnabled="False" IsTextPredictionEnabled="False" HandlerChanged="Entry_HandlerChanged" />
后台C#代码
private void Entry_HandlerChanged(object sender, EventArgs e) { var entry = sender as Entry; var handler = entry?.Handler; if (handler == null) return; #if IOS if (handler.PlatformView is UIKit.UITextField textField) { textField.AutocorrectionType = UIKit.UITextAutocorrectionType.No; textField.SpellCheckingType = UIKit.UITextSpellCheckingType.No; } #elif ANDROID if (handler.PlatformView is Android.Widget.EditText editText) { editText.InputType = Android.Text.InputTypes.TextFlagNoSuggestions | Android.Text.InputTypes.ClassText; } #elif WINDOWS if (handler.PlatformView is Microsoft.UI.Xaml.Controls.TextBox textBox) { Microsoft.UI.Xaml.Controls.SpellCheck.SetIsEnabled(textBox, false); textBox.InputScope = new Microsoft.UI.Xaml.Input.InputScope(); textBox.InputScope.Names.Add(new Microsoft.UI.Xaml.Input.InputScopeName(Microsoft.UI.Xaml.Input.InputScopeNameValue.Text)); } #endif }
平台说明
- iOS平台:通过修改
UITextField的AutocorrectionType和SpellCheckingType彻底关闭拼写相关功能 - Android平台:设置
InputType为TextFlagNoSuggestions来禁用文本预测和拼写建议 - Windows平台:关闭
SpellCheck并设置InputScope为普通文本,避免系统自动启用预测功能
内容的提问来源于stack exchange,提问作者Enrico
相关产品推荐
相关产品推荐

