更改AutoSuggestBox的触摸键盘布局(InputScope)

时间:2017-06-18 06:48:26

标签: c# uwp

在UWP中,有一个可用于TextBox和RichEditBox的InputScope方法,可用于为不同类型的输入定义键盘布局。 但问题是AutoSuggestBox没有InputScope。我正在使用AutoSuggestBox输入URI,因此我需要在触摸键盘上显示“.com”按钮。

我知道AutoSuggestBox派生自TextBox类(可能),所以它应该与inputScope方法一起使用。我用过这个黑客 -

 private void AutoSuggestBox_GotFocus(object sender, RoutedEventArgs e)
    {
        InputScope keyType = new InputScope();
        InputScopeName typeOfKeyboard = new InputScopeName();
        typeOfKeyboard.NameValue = InputScopeNameValue.Url;
        keyType.Names.Add(typeOfKeyboard);
        (e.OriginalSource as TextBox).InputScope = keyType;  
    }

但它仍然无效。是否有任何替代解决方案,因为我不想用任何其他输入类型替换ASB。

2 个答案:

答案 0 :(得分:1)

第一次AutoSuggestBox获得焦点后,您的代码段应该有效。在AutoSuggestBox第一次聚焦之前,InputScope是默认值,因此当它首次聚焦时,新的输入范围不会生效,从第二次聚焦起它将生效。

AutoSuggestBox实际上由TextBoxPopup组成。要更改AutoSuggestBox的输入范围,实际上是要更改TextBox内部AutoSuggestBox的输入范围。因此,请更新AutoSuggestBox的模板,如下所示:

<AutoSuggestBox x:Name="autosuggestbox">
    <AutoSuggestBox.Style>
        <Style TargetType="AutoSuggestBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="AutoSuggestBox">
                        <Grid x:Name="LayoutRoot">
                            <TextBox
                                x:Name="TextBox"
                                Width="{TemplateBinding Width}"
                                Margin="0"
                                Canvas.ZIndex="0"
                                DesiredCandidateWindowAlignment="BottomEdge"
                                Header="{TemplateBinding Header}"
                                InputScope="Url"
                                PlaceholderText="{TemplateBinding PlaceholderText}"
                                ScrollViewer.BringIntoViewOnFocusChange="False"
                                Style="{TemplateBinding TextBoxStyle}" />
                            <Popup x:Name="SuggestionsPopup">
                                <Border x:Name="SuggestionsContainer">
                                    <Border.RenderTransform>
                                        <TranslateTransform x:Name="UpwardTransform" />
                                    </Border.RenderTransform>
                                    <ListView
                                        x:Name="SuggestionsList"
                                        MaxHeight="{ThemeResource AutoSuggestListMaxHeight}"
                                        Margin="{ThemeResource AutoSuggestListMargin}"
                                        Padding="{ThemeResource AutoSuggestListPadding}"
                                        Background="{ThemeResource AutoSuggestBoxSuggestionsListBackground}"
                                        BorderBrush="{ThemeResource AutoSuggestBoxSuggestionsListBorderBrush}"
                                        BorderThickness="{ThemeResource AutoSuggestListBorderThemeThickness}"
                                        DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
                                        IsItemClickEnabled="True"
                                        ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                                        ItemTemplate="{TemplateBinding ItemTemplate}"
                                        ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}">
                                        <ListView.ItemContainerTransitions>
                                            <TransitionCollection />
                                        </ListView.ItemContainerTransitions>
                                    </ListView>
                                </Border>
                            </Popup>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="Orientation">
                                    <VisualState x:Name="Landscape" />
                                    <VisualState x:Name="Portrait" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </AutoSuggestBox.Style>
</AutoSuggestBox>

更多详情请参阅AutoSuggestBox styles and templates

答案 1 :(得分:0)

您可以修改AutoSuggestBox的模板,并将InputScope设置为XAML中的TextBoxHere您可以找到AutoSuggestBox的默认模板。