Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...