WPF 禁用TextBox的触摸后自动弹出虚拟键盘

前言 & 问题

如下截图,TextBox,在触摸点击后,会自动弹出windows的虚拟键盘。

如何,禁用键盘的自动弹出?

调用虚拟键盘

通过调用TapTip.exe或者osk.exe,主动弹出虚拟键盘

详细调用可参考:c#调用windows虚拟键盘

如何禁用键盘的弹出

TextBox在触摸点击后,会自动弹出虚拟键盘,是因为在控件中作了封装。

处理方案:重写TextBox的方法OnCreateAutomationPeer,返回一个UIElementAutomationPeer而不是AutomationPeer。

可能原因:TextBox自定义实现中返回的是AutomationPeer,而UIElementAutomationPeer继承AutomationPeer,重写了相关键盘属性。

猜测与HasKeyboardFocusCore属性有关。

方案参考:

 1     /// <summary>
 2     /// 禁用自动弹出虚拟键盘的TextBox控件
 3     /// </summary>
 4     public class TextBoxNoAutoKeyboard : TextBox
 5     {
 6         protected override AutomationPeer OnCreateAutomationPeer()
 7         {
 8             return new FrameworkElementAutomationPeer(this);
 9         }
10     }

 以上参考自:【stackoverflow】“Hide” text box from automatic Win10 keyboard showing

posted @ 2018-12-21 10:44  唐宋元明清2188  阅读(1845)  评论(0编辑  收藏  举报