简体   繁体   English

Windows 8 - 如何解除触摸键盘?

[英]Windows 8 - How to Dismiss Touch Keyboard?

I am developing my app for Windows 8 in C#, and one very annoying thing is that the touch keyboard sometimes stays on screen even though all textboxes have lost focus. 我正在用C#开发我的Windows 8应用程序,一个非常令人讨厌的事情是触摸键盘有时会停留在屏幕上,即使所有文本框都失去焦点。

I read the article keyboard dismissal logic white paper , which explains that when switching from control to control, the keyboard can stay on even though a control may not accept keyboard input. 我阅读了文章键盘解雇逻辑白皮书 ,它解释了当从控制切换到控制时,即使控件可能不接受键盘输入,键盘也可以保持打开状态。 This would be my case because all my contents are hosted in either a GridView or a ListView. 这将是我的情况,因为我的所有内容都托管在GridView或ListView中。 When the user clicks on any item on screen, the tap would land on these controls. 当用户点击屏幕上的任何项目时,点击将落在这些控件上。 This is very annoying because the keyboard takes half of a screen and there is no way to close the keyboard. 这非常烦人,因为键盘需要一半的屏幕,而且无法关闭键盘。

I have tried to set the textbox to be disabled and it had not affect. 我试图将文本框设置为禁用,但它没有影响。 The only way to remove the keyboard is to set focus on a button, which is extremely hacky. 删除键盘的唯一方法是将焦点设置在按钮上,这非常黑客。

I thought I needed to do something with the "AutomationPeer", but I am not clear what exactly to do. 我认为我需要对“AutomationPeer”做一些事情,但我不清楚究竟要做什么。 Is there a way to override this behavior? 有没有办法覆盖这种行为?

Edit: I figured this out. 编辑:我想出来了。 The goal is to change to the control type of the GridView and GridView item not listed in the whitepaper . 目标是更改为白皮书中未列出的GridView和GridView项的控件类型。 Here is the code of the grid that I did that allowed me to dismiss the keyboard: 这是我做的网格代码,允许我解雇键盘:

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);
        }


    }

    protected override AutomationPeer OnCreateAutomationPeer()
    {
        var baseItem = base.OnCreateAutomationPeer();
        return new KeyboardUnfocusableGridViewAutomationPeer(this);
    }

    protected override Windows.UI.Xaml.DependencyObject GetContainerForItemOverride()
    {
        return new KeyboardUnfocusableGridViewItem();
    }
}

It's unfortunate that I need to write this much code to do a simple thing. 不幸的是,我需要编写这么多代码来做一件简单的事情。 This is definitely not optimal since I would need to do this for each of the ItemsControl that I need to use. 这绝对不是最佳的,因为我需要为我需要使用的每个ItemsControl执行此操作。

What you need to do is set focus to any control that does not accept text entry. 您需要做的是将焦点设置为任何不接受文本输入的控件。 However, be aware that if the user manually showed the keyboard (as opposed to it automatically showing because a TextBox received focus) then the keyboard will remain open. 但是,请注意,如果用户手动显示键盘(而不是因为TextBox接收到焦点而自动显示),则键盘将保持打开状态。

Check out this really good thread about the on-screen keyboard for more info: 有关更多信息,请查看有关屏幕键盘的这个非常好的主题:

http://social.msdn.microsoft.com/Forums/pl/winappswithcsharp/thread/3c227262-1d2c-4382-9c50-5b71d2b5d823 http://social.msdn.microsoft.com/Forums/pl/winappswithcsharp/thread/3c227262-1d2c-4382-9c50-5b71d2b5d823

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM