在 Windows 的 uwp 应用程序中关闭软键盘 [英] Turn off soft keyboard in uwp app for windows

查看:29
本文介绍了在 Windows 的 uwp 应用程序中关闭软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 uwp 应用程序在运行 Windows 10 的手持设备上运行.手持设备有一个条形码扫描仪,应用程序的所有输入都是使用它进行的.所以我想防止当用户将焦点移动到任何文本框控件时键盘出现.

I have an uwp app running on a handheld device running windows 10. The handheld device has a barcode scanner and all input for the app is made using this. So I want to prevent the keyboard coming up when a user moves the focus to any of the textbox controls.

在很大程度上,焦点是以编程方式处理的 - 我已经通过 PreventKeyboardDisplayOnProgrammaticFocus=True 阻止了在这些情况下出现的键盘.但是用户有时确实需要自己移动焦点,当他这样做时,我找不到任何防止键盘出现的方法.

To a large extent, the focus is handled programmatically - and I have prevented the keyboard coming up in those instances with PreventKeyboardDisplayOnProgrammaticFocus=True. But the user does need to move the focus himself sometimes and I cannot find any way of preventing the keyboard coming up when he does this.

我找到了有关上述编程焦点的文章,并在用户在文本框中按下 Enter 键时隐藏键盘 - 并将控件的 readonly 值设置为 true.但这些在这种情况下并不适用.我希望能够防止它出现在这个应用程序中.谁能给点建议?

I have found articles regarding the programmatic focus mentioned above, and hiding the keyboard when the user presses enter in a textbox - and setting the readonly value to true for the control. But these are not applicable in this case. I want to be able to prevent it coming up ever in this app. Can anyone advise?

推荐答案

我不确定是否有直接的方法来防止键盘出现.通过订阅 InputPane 的 事件,您可以在键盘显示后隐藏它:

I'm not sure if there is a direct way to prevent keyboard from showing up. You can surely hide the keyboard once it shows, by subscribing to InputPane's events:

InputPane.GetForCurrentView().Showing += (s, e) => (s as InputPane).TryHide();

但这看起来不太好.因此,我尝试了一种棘手的方法来实现您想要的目标 - 禁用 TextBox 进行命中测试并在其下使用虚拟控件来设置编程焦点.正如我已经测试过的,它应该可以工作.示例 xaml:

But this doesn't look nice. Therefore I've tried a tricky way to achieve what you want - disable the TextBox for hit testing and use dummy control under it to set programmatic focus. As I've tested it should work. The sample xaml:

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Border Tapped="Border_Tapped" Background="Transparent">
        <TextBox x:Name="myTextBox" Width="200" Height="100" Header="Enter:" PreventKeyboardDisplayOnProgrammaticFocus="True" IsHitTestVisible="False"/>
    </Border>
    <Button Margin="20" Content="Dummy to test focus"/>
</StackPanel>

以及背后的代码:

private void Border_Tapped(object sender, TappedRoutedEventArgs e)
{
    myTextBox.Focus(FocusState.Programmatic);
}

这篇关于在 Windows 的 uwp 应用程序中关闭软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆