简体   繁体   English

如何在WP7中隐藏软键盘?

[英]how to hide soft keyboard in WP7?

In a TextBox input. 在TextBox输入中。 After type enter key, i want to hide soft keyboard. 输入密钥后,我想隐藏软键盘。 How to do it in codes? 怎么做代码?

private void OnKeyDownHandler(object sender, KeyEventArgs e)
           {
                if (e.Key != Key.Enter)
                   return;         

...}

this.focus() This will allow the focus to be lost from the textbox. this.focus()这将允许焦点从文本框中丢失。 It basically puts the focus on the page instead. 它基本上把重点放在页面上。 You could also convert your textbox to read only to disallow any further input. 您还可以将文本框转换为read only以禁止任何进一步的输入。

Hiding the SIP can be done by simply changing the focus from the textbox to any other element on the page. 只需将焦点从文本框更改为页面上的任何其他元素,即可隐藏SIP。 It does not have to be this.focus(), it could be anyElement.focus(). 它不一定是this.focus(),它可以是anyElement.focus()。 As long as the element is not your textbox, the SIP should hide itself. 只要元素不是您的文本框,SIP就应该隐藏自己。

I use the following method to dismiss the SIP: 我使用以下方法来解雇SIP:

/// 
/// Dismisses the SIP by focusing on an ancestor of the current element that isn't a
/// TextBox or PasswordBox.
/// 
public static void DismissSip()
{
    var focused = FocusManager.GetFocusedElement() as DependencyObject;

    if ((null != focused) && ((focused is TextBox) || (focused is PasswordBox)))
    {
        // Find the next focusable element that isn't a TextBox or PasswordBox
        // and focus it to dismiss the SIP.
        var focusable = (Control)(from d in focused.Ancestors()
                                  where
                                    !(d is TextBox) &&
                                    !(d is PasswordBox) &&
                                    d is Control
                                  select d).FirstOrDefault();
        if (null != focusable)
        {
            focusable.Focus();
        }
    }
 }

The Ancestors method comes from LinqToVisualTree by Colin Eberhardt. Ancestors方法来自Colin Eberhardt的LinqToVisualTree The code is used in conjunction with an Enter key handler, for "tabbing" to the next TextBox or PasswordBox, which is why they're skipped in the selection, but you could include them if it makes sense for you. 该代码与Enter键处理程序一起使用,用于“tabbing”到下一个TextBox或PasswordBox,这就是为什么它们在选择中被跳过的原因,但如果它对你有用,你可以包括它们。

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

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