如何禁用 AutoSuggestBox 上的拼写检查器

How to disable the spell checker on an AutoSuggestBox

我想在 Windows Phone 8.1 应用程序中禁用拼写检查器,它在 AutoSuggestBox 上默认打开,但不需要:

控件的标记:

<AutoSuggestBox 
    Name="txtOrgunit" 
    TextChanged="txtOrgunit_TextChanged"
    SuggestionChosen="txtOrgunit_SuggestionChosen">
</AutoSuggestBox>

如何通过标记或代码实现 IsSpellCheckEnabled 属性 在内部文本框中变为 false?

我发现的现有解决方案要么在其他平台上处理相同的问题(例如:

How can I disable the spell checker on text inputs on the iPhone

还有这个:

how to disable spell checker for Android AutoCompleteTextView?)

或者它们是笨拙的火箭科学,像这样:

https://social.msdn.microsoft.com/Forums/windowsapps/en-US/c2139520-26e9-4a74-819d-defb4e20857c/how-to-disable-spell-check-grammer-in-autosuggestbox?forum=wpdevelop

编辑:逐字应用第一个答案中提出的解决方案后,实现了 OP 目标,但控件的功能被破坏了(引发事件,itemssource最终有 30 个项目,但显示了其中的 none - "dropdown" 不再出现)。因此,我在下面给出了 txtOrgunit_TextChanged 处理程序的源代码:

private void txtOrgunit_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
    if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
    {
        var ui = sender.Text.Trim().ToUpperInvariant();
        var matches = new List<IdAndCaption>();
        var count = 0;
        for (int i = 0; i < Com.MasterdataBasic.Orgunits.Length; ++i)
        {
            var cand = Com.MasterdataBasic.Orgunits[i];
            var cap = String.Format("{0} {1}", cand.Abbrev, cand.LongCap);
            if (cap.ToUpperInvariant().Contains(ui))
            {
                var ele = new IdAndCaption() { Id = cand.OrgID, Caption = cap };
                matches.Add(ele);
                ++count;
                /* UX decided it unreasonable to have the user scroll through more...
                 * should type more letters to restrict further */
                if (count >= 30) break;
            }
        }
        sender.ItemsSource = matches;
        Rec.Report.OrgID = -1;
    }    
}

我确认当我从自动建议框中删除样式标签时,自动建议功能会恢复。

不幸的是 AutoSuggestBox 控件似乎没有 属性 IsSpellCheckEnabled 所以为了做到这一点你需要创建一个 ControlTemplateTextBox 控件确实包含 属性 IsSpellCheckEnabled 并在那里将其设置为 False

你首先要做的是在你的项目中创建一个 ResourceDictionary 如果你还没有的话:

为了这个例子的目的,我给了我的名字 Styles.xaml.

下面的代码将或多或少地满足您的需求。您可能想要调整某些 Setter 属性,但脱离了您在问题中提供的 link 中的示例,我拼凑了这个:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppNamespace">

<!-- modified default style for AutoSuggestBox -->
<Style x:Name="AutoSuggestBoxStyle" TargetType="AutoSuggestBox">
    <Setter Property="Margin" Value="{ThemeResource TextControlMarginThemeThickness}" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListViewItem">
                <Setter Property="Margin" Value="{ThemeResource AutoSuggestListViewItemMargin}" />
                <Setter Property="FontSize" Value="{ThemeResource ContentControlFontSize}" />
                <Setter Property="Foreground" Value="{ThemeResource TextBoxForegroundThemeBrush}" />
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="AutoSuggestBox">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="Orientation">
                            <VisualState x:Name="Landscape"/>
                            <VisualState x:Name="Portrait"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <TextBox x:Name="TextBox"
                         IsSpellCheckEnabled="False"
                         PlaceholderText="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PlaceholderText}"
                         Header="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Header}"
                         Width="{TemplateBinding Width}"
                         ScrollViewer.BringIntoViewOnFocusChange="False"
                         Canvas.ZIndex="0"
                         Margin="0" />
                    <Popup x:Name="SuggestionsPopup">
                        <Border x:Name="SuggestionsContainer"
                              Background="{ThemeResource AutoSuggestBackgroundThemeBrush}"
                              BorderBrush="{ThemeResource PhoneAccentBrush}"
                              BorderThickness="{ThemeResource TextControlBorderThemeThickness}">
                            <Border.RenderTransform>
                                <TranslateTransform x:Name="UpwardTransform"/>
                            </Border.RenderTransform>
                            <ListView x:Name="SuggestionsList"
                              ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource}"
                              ItemTemplate="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemTemplate}"
                              ItemTemplateSelector="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemTemplateSelector}"
                              ItemContainerStyle="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemContainerStyle}"
                              RenderTransformOrigin=".5,.5">
                                <ListView.RenderTransform>
                                    <ScaleTransform x:Name="ListItemOrderTransform"/>
                                </ListView.RenderTransform>
                            </ListView>
                        </Border>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

将XAML复制到Styles.xaml.

您现在需要引用这个新 ResourceDictionary。这可以在您的 App.xaml 或页面本身中完成。

App.xaml 中,您将这样参考:

<Application
    x:Class="App6.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App6">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

If for whatever reason you can't get to the XAML in your App.xaml then do a search for <Application and you should be able to get to it that way.

然后在页面本身中您可以创建一个 AutoSuggestBox 并引用 AutoSuggestBoxStyle:

<AutoSuggestBox Style="{StaticResource AutoSuggestBoxStyle}"/>

在我的示例中,我有两个 AutoSuggestBoxes:

<StackPanel>
    <AutoSuggestBox Style="{StaticResource AutoSuggestBoxStyle}"/>
    <AutoSuggestBox></AutoSuggestBox>
</StackPanel>

这是它在我的模拟器中的样子:

如您所见,顶部 AutoSuggestBox 对样式的引用不像底部那样显示红线。

希望这对您有所帮助。