如何在WP7中隐藏软键盘?

4
在一个文本框输入后,按下回车键后,我想隐藏软键盘。如何在代码中实现?
private void OnKeyDownHandler(object sender, KeyEventArgs e)
           {
                if (e.Key != Key.Enter)
                   return;         

...}
2个回答

27

this.focus() 此代码可使光标从文本框中移开,将焦点放在页面上。你也可以将文本框转换为只读模式,以防止进一步的输入。

隐藏SIP可以通过将焦点从文本框切换到页面上的任何其他元素来完成。不一定非要使用this.focus(),也可以是任何一个元素,只要不是你的文本框,SIP就会自动隐藏。


谢谢。但我无法使用它,因为我只找到了SearchTxt.Focus(),这意味着获取焦点,但没有设置TextBox的焦点。 - whi
隐藏SIP可以通过将焦点从文本框转移到页面上的任何其他元素来实现。它不一定是this.focus(),也可以是anyElement.focus()。只要该元素不是您的文本框,SIP就应该会自动隐藏。 - abhinav
this.Focus() 对我没有起作用。我不得不在页面上创建一个新的宽度为0的虚拟 Button,然后将焦点放在那里。 - RandomEngy

2

我使用以下方法来关闭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();
        }
    }
 }
< p > Ancestors 方法来自 Colin Eberhardt 的 LinqToVisualTree。该代码与 Enter 键处理程序一起使用,用于“制表符”到下一个 TextBox 或 PasswordBox,这就是为什么它们在选择中被跳过的原因,但如果对您有意义,您可以包括它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接