Windows 上的搜索栏拼写检查

此特定于通用 Windows 平台的平台特定功能使 SearchBar 能够与拼写检查引擎进行交互。 通过将 SearchBar.IsSpellCheckEnabled 附加属性设置为 boolean 值在 XAML 中使用它:

<ContentPage ...
             xmlns:windows="clr-namespace:Xamarin.Forms.PlatformConfiguration.WindowsSpecific;assembly=Xamarin.Forms.Core">
    <StackLayout>
        <SearchBar ... windows:SearchBar.IsSpellCheckEnabled="true" />
        ...
    </StackLayout>
</ContentPage>

或者,可以使用 Fluent API 从 C# 使用它:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
...

searchBar.On<Windows>().SetIsSpellCheckEnabled(true);

SearchBar.On<Windows> 方法指定此特定于平台的功能仅在通用 Windows 平台上运行。 Xamarin.Forms.PlatformConfiguration.WindowsSpecific 命名空间中的 SearchBar.SetIsSpellCheckEnabled 方法打开和关闭拼写检查器。 此外,SearchBar.SetIsSpellCheckEnabled 方法还可通过调用 SearchBar.GetIsSpellCheckEnabled 方法返回是否启用了拼写检查器来切换拼写检查器:

searchBar.On<Windows>().SetIsSpellCheckEnabled(!searchBar.On<Windows>().GetIsSpellCheckEnabled());

结果是可以对输入到 SearchBar 中的文本进行拼写检查,并向用户指示不正确的拼写:

特定平台的 SearchBar 拼写检查

注意

Xamarin.Forms.PlatformConfiguration.WindowsSpecific 命名空间中的 SearchBar 类还具有 EnableSpellCheckDisableSpellCheck 方法,可分别用于启用和禁用 SearchBar 上的拼写检查器。