我正在用C#开发我的Windows 8应用程序,一个非常令人讨厌的事情是触摸键盘有时会停留在屏幕上,即使所有文本框都失去焦点.
我阅读了文章键盘解雇逻辑白皮书,它解释了当从控制切换到控制时,即使控件可能不接受键盘输入,键盘也可以保持打开状态.这将是我的情况,因为我的所有内容都托管在GridView或ListView中.当用户点击屏幕上的任何项目时,点击将落在这些控件上.这非常烦人,因为键盘需要一半的屏幕,而且无法关闭键盘.
我试图将文本框设置为禁用,但它没有影响.删除键盘的唯一方法是将焦点设置在按钮上,这非常黑客.
我认为我需要对"AutomationPeer"做些什么,但我不清楚究竟要做什么.有没有办法覆盖这种行为?
编辑: 我想出来了.目标是更改为白皮书中未列出的GridView和GridView项的控件类型.这是我做的网格代码,允许我解雇键盘:
public class KeyboardUnfocusableGridView : GridView
{
private class KeyboardUnfocusableGridViewAutomationPeer : GridViewAutomationPeer
{
public KeyboardUnfocusableGridViewAutomationPeer(GridView owner)
: base(owner)
{
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
}
private class KeyboardUnfocusableGridViewItemAutomationPeer : GridViewItemAutomationPeer
{
public KeyboardUnfocusableGridViewItemAutomationPeer(GridViewItem owner)
: base(owner)
{ }
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
}
private class KeyboardUnfocusableGridViewItem : GridViewItem
{
protected override AutomationPeer OnCreateAutomationPeer()
{
var baseItem = base.OnCreateAutomationPeer();
return new KeyboardUnfocusableGridViewItemAutomationPeer(this);
} …Run Code Online (Sandbox Code Playgroud)