如何隐藏WP7中的软键盘?
问题描述:
在TextBox输入中。 键入确认键后,我想隐藏软键盘。 如何在代码中做到这一点?如何隐藏WP7中的软键盘?
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter)
return;
...}
答
this.focus()
这将允许从文本框失去焦点。它基本上把重点放在页面上。您也可以将您的文本框转换为read only
以禁止进一步输入。
隐藏SIP可以通过将焦点从文本框更改为页面上的任何其他元素来完成。它不一定是this.focus(),它可以是anyElement.focus()。只要该元素不是您的文本框,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();
}
}
}
的Ancestors
方法来自LinqToVisualTree科林·埃伯哈特。该代码与Enter键处理程序一起使用,用于“Tabbing”到下一个TextBox或PasswordBox,这就是为什么它们在选择中被跳过的原因,但如果它适合您,则可以包含它们。
谢谢。但我无法使用它。因为我只找到this.SearchTxt.Focus()这意味着获得焦点。但TextBox没有设置焦点。 – whi
那么,只需将焦点从文本框更改为页面上的任何其他元素即可隐藏SIP。它不必是'this.focus()',它可以是'anyElement.focus()'。只要该元素不是您的文本框,SIP应该隐藏自己。 – abhinav
Got it!它的工作原理,谢谢。 – whi