本文介绍了在 windows8 上以编程方式显示/隐藏键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式在 Windows Metro 应用程序上显示/隐藏键盘.我最初认为我可以使用折叠的文本框并将焦点放在它上面.但在这个 link 中似乎不允许这样做.该链接还讨论了实现此目的的 AutomationPeer 和 TextAutomationPeer.有没有关于如何使用这些的资源?

I am trying to show/hide keyboard on a Windows Metro app programmatically. I initially thought I could do it using a collapsed textbox and setting focus on it. But it seems like that has been disallowed in this link. The link also talks about the AutomationPeer and TextAutomationPeer to accomplish this. Is there a resource on how to use these ?

提前致谢PK

推荐答案

来自 这里:

UI 自动化是开发人员交流的机制特定的 UI 元素是否可以接收文本输入.你必须确保在您的应用程序,以便触摸键盘知道焦点时出现落在特定的 UI 元素上.对于 Windows 提供的控件,此将自动完成,因为适当的可访问性属性是默认设置,但对于自定义控件和体验,您必须执行正确设置可访问性属性的额外工作;请记住,触摸键盘会对这些属性做出反应.

如果您使用 C# 或 C++,请使用 AutomationPeer 对象,特别是 TextAutomationPeer.Windows 8 Release Preview 示例将演示如何在 C# 中执行此操作.请记住,控件还必须可编辑并且能够接收文本以使键盘调用,在除了具有适当的辅助功能设置.表示某些东西可以接收文本时它不能会误导无障碍工具和依赖它们的用户.

If you use C# or C++, use an AutomationPeer object, and specifically a TextAutomationPeer. A Windows 8 Release Preview sample will demonstrate how to do this in C#. Remember that the control must also be editable and able to receive text to get the keyboard to invoke, in addition to having the appropriate accessibility settings. Indicating that something can receive text when it cannot will mislead accessibility tools and the users who rely on them.

为了启用用户驱动的调用,我们跟踪最后一个坐标触摸事件并将它们与边界矩形的位置进行比较当前具有焦点的元素.如果包含该点在边界矩形内,触摸键盘被调用.

To enable user-driven invocation, we track the coordinates of the last touch event and compare them to the location of the bounding rectangle of the element that currently has focus. If the point is contained within the bounding rectangle, the touch keyboard is invoked.

所以您无法以编程方式显示键盘.隐藏/显示键盘的适当方法是将控件设置为使用 AutomationPeer 对象接受输入.

So you cannot programmatically show the keyboard. The appropriate way to hide/show the keyboard is by setting your control to accept input using an AutomationPeer object.

这里,如果您将输入控件设置为只读,则它不会触发键盘,因此您可以使用它来控制键盘何时打开.

From here, if you set the input control to read-only then it will not trigger the keyboard, so possibly you could use that to control when the keyboard opens.

在实现文本自动化对等体时需要检查几件事:

There are a few things to check when implementing the text automation peer:

  1. 确保您使用真实的触控设备进行测试,或者使用带有基本触控模式工具的模拟器进行测试.如果您不这样做,则自动化对等将不会激活,因为它仅由手写笔或触摸输入(不是鼠标)激活.

  1. Make sure you either test with a real touch device or test using the simulator with the Basic Touch Mode tool. If you don't do this the automation peer won't activate since it is only activated by a stylus or a touch input (not mouse).

确保您的自定义控件实现 OnCreateAutomationPeer 类似这样的内容:

Make sure your custom control implements OnCreateAutomationPeer something like this:

受保护的覆盖 AutomationPeer OnCreateAutomationPeer(){返回新的 CustomControl2AutomationPeer(this);}

protected override AutomationPeer OnCreateAutomationPeer(){ return new CustomControl2AutomationPeer(this);}

更多详细信息请参见示例此处.

More details found in the example here.

这篇关于在 windows8 上以编程方式显示/隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 11:22