Windows 8 - 如何关闭触控键盘? [英] Windows 8 - How to Dismiss Touch Keyboard?

查看:20
本文介绍了Windows 8 - 如何关闭触控键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 为 Windows 8 开发我的应用程序,一件非常烦人的事情是,即使所有文本框都失去焦点,触摸键盘有时也会停留在屏幕上.

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.

我阅读了文章键盘解除逻辑白皮书,它解释了当从一个控件切换到另一个控件时,即使控件可能不接受键盘输入,键盘也可以保持打开状态.这就是我的情况,因为我的所有内容都托管在 GridView 或 ListView 中.当用户点击屏幕上的任何项目时,点击会落在这些控件上.这很烦人,因为键盘占了半个屏幕,而且没有办法关闭键盘.

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. 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.

我尝试将文本框设置为禁用,但没有影响.移除键盘的唯一方法是将焦点设置在一个按钮上,这非常hacky.

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.

我认为我需要用AutomationPeer"做点什么,但我不清楚具体要做什么.有没有办法覆盖这种行为?

I thought I needed to do something with the "AutomationPeer", but I am not clear what exactly to do. Is there a way to override this behavior?

我想通了.目标是更改为 白皮书.这是我所做的允许我关闭键盘的网格代码:

I figured this out. The goal is to change to the control type of the GridView and GridView item not listed in the whitepaper. 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();
    }
}

很遗憾,我需要编写这么多代码来做一件简单的事情.这绝对不是最佳选择,因为我需要为每个需要使用的 ItemsControl 执行此操作.

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.

推荐答案

您需要做的是将焦点设置到任何不接受文本输入的控件.但是请注意,如果用户手动显示键盘(而不是因为 TextBox 获得焦点而自动显示),那么键盘将保持打开状态.

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.

查看这个关于屏幕键盘的非常好的帖子了解更多信息:

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

这篇关于Windows 8 - 如何关闭触控键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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