如何在EditText软键盘上隐藏8个Metro应用程序?

我使用C#在我的Frame中有一个EditText和一个Button。 在编辑字段内写入并单击按钮后,我想隐藏虚拟软键盘。

你不能。 有关输入主机管理器和软键盘行为的更多信息,您可以注册以了解它何时显示或隐藏 。 但是,您无法以编程方式控制它是向上还是向下。

添加一个虚拟按钮并将焦点设置到它,键盘将被隐藏。

谢谢你的提问。 我已经为这个问题找到了更好的解决方案。 像这样

首先我们可以在xaml中添加处理程序

  ......  

然后我们关注当前页面如下。 它运作良好。

 private void Grid_Tapped_1(object sender, TappedRoutedEventArgs e) { this.Focus(FocusState.Programmatic); } 

当显示虚拟键盘的文本框将其functionIsEnabled设置为false时,虚拟键盘将消失。 之后我们可以立即设置为true,虚拟键盘将保持隐藏状态。 像这样:

 MyTextBox.KeyDown += (s, a) => { if (a.Key == VirtualKey.Enter) { MyTextBox.IsEnabled = false; MyTextBox.IsEnabled = true; } }; 

尝试设置Textbox`的IsReadOnly属性。

我正在做一些“相似”的事情

  private void textbox_input_LostFocus(object sender, RoutedEventArgs e) { textbox_input.IsReadOnly = false; } private void textbox_input_Tapped(object sender, TappedRoutedEventArgs e) { if(e.PointerDeviceType != Windows.Devices.Input.PointerDeviceType.Mouse) textbox_input.IsReadOnly = true; else textbox_input.IsReadOnly = false; } 

如果用户没有使用鼠标,我会使用此剪辑来抑制键盘…

此外,当文本框是只读时,会触发KeyDown事件,因此您可以直接使用数据来设置视图模型并在其上更新文本框;)

有一个解决方案可以通过在单击按钮作为“提交”后自动设置容器的IsTabStop=true来隐藏触摸键盘。

但是,顺便说一句,我注意到下次进入该页面时, EditText (应该是一个TextBox )将自动聚焦,并显示触摸键盘。 也许你最好在提交后禁用EditText 。 (似乎完成并阻止输入操作)

我有同样的问题,只有一点点差异。 当我从文本框切换到日期选择器时,软键盘不会消失。

我尝试了你所有的建议,但没有任何效果。 每次我的datepicker都有一个奇怪的行为,在我尝试上述解决方案之一(或其他一些stackoverflow线程)之后。

过了一段时间后,我通过谷歌找到了一些东西,就像一个魅力。 这里

在评论部分Dusher16写了一个非常干净的解决方案,它也适用于WinRT / Win8 / Win8.1 / Metro或如何调用它。

创建一个新类:

 using System.Runtime.InteropServices; using Windows.Devices.Input; namespace Your.Namespace { public static class TouchKeyboardHelper { #region < Attributes > private const int WmSyscommand = 0x0112; // Flag to received/send messages to the system. private const int ScClose = 0xF060; // Param to indicate we want to close a system window. #endregion < Attributes > #region < Properties > public static bool KeyboardAttached { get { return IsKeyboardAttached(); } } #endregion < Properties > #region < Methods > [DllImport("user32.dll")] private static extern int FindWindow(string lpClassName, string lpWindowName); // To obtain an active system window handler. [DllImport("user32.dll")] private static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam); // To send a message to the system. ///  /// To detect if a real keyboard is attached to the dispositive. ///  ///  private static bool IsKeyboardAttached() { KeyboardCapabilities keyboardCapabilities = new KeyboardCapabilities(); // To obtain the properties for the real keyboard attached. return keyboardCapabilities.KeyboardPresent != 0 ? true : false; } ///  /// To close the soft keyboard ///  public static void CloseOnscreenKeyboard() { // Retrieve the handler of the window int iHandle = FindWindow("IPTIP_Main_Window", ""); // To find the soft keyboard window. if (iHandle > 0) { SendMessage(iHandle, WmSyscommand, ScClose, 0); // Send a close message to the soft keyboard window. } } #endregion < Methods > } } 

例如,在某些XAML.cs文件中添加以下行:

 private void DatePicker_GotFocus(object sender, RoutedEventArgs e) { if (TouchKeyboardHelper.KeyboardAttached) TouchKeyboardHelper.CloseOnscreenKeyboard(); }