2012-05-23 50 views
1

我在使用C#的框架中有一个EditText和一个按钮。在编辑区内写入并单击按钮后,我想要隐藏虚拟软键盘。如何隐藏EditText软键盘windows 8 Metro应用程序?

+0

这是不可能的Win8 CP。可能的重复:http://stackoverflow.com/questions/10129550/show-hide-keyboard-programmatically-on-windows8 –

+0

如果在这个问题上的任何其他选择。 – Narasimha

回答

2

你不能。有关于Input Hosting Manager and Soft Keyboard的行为的更多信息,您可以register to know when it shows or becomes hidden。但是,您无法以编程方式控制它是启动还是关闭。

+1

我不明白为什么这是被接受的答案。按照罗伯特的建议,将焦点放在隐藏的按钮上,完美地工作。一个人必须抓住一些角落的情况,但最终的用户体验要好得多...... –

0

尝试设置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事件而解雇文本框是只读的,因此你可以直接利用这些数据来设置您的视图模型,并更新了它的文本框;)

7

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

+0

我这样做,但设置了“IsTabStop =”true“'并将'Height' /'Width'设置为'”0“',而不是'可见性=“折叠”'。这是我对环境的神奇结合。 –

3

感谢您的问题。 我已经为这个问题得到了更好的解决方案。这样

首先我们可以在XAML

<Grid x:Name= Tapped="Grid_Tapped_1"> 
    ...... 
</Grid > 

添加处理程序,然后我们注重当前页面类似如下。它运作良好。

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

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

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

There是可以通过设置隐藏触摸键盘溶液的容器的IsTabStop=true点击您的按钮为“提交”后,全自动。

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

0

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

我尝试了所有的你的建议,但是毫无效果像它应该。每次我的datepicker有一个奇怪的行为后,我尝试了上述解决方案之一(或其他一些其他stackoverflow线程)。

一段时间后,我发现通过谷歌的东西,它的工作就像一个魅力。 HERE

在评论部分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. 

     /// <summary> 
     /// To detect if a real keyboard is attached to the dispositive. 
     /// </summary> 
     /// <returns></returns> 
     private static bool IsKeyboardAttached() 
     { 
      KeyboardCapabilities keyboardCapabilities = new KeyboardCapabilities(); // To obtain the properties for the real keyboard attached. 
      return keyboardCapabilities.KeyboardPresent != 0 ? true : false; 
     } 

     /// <summary> 
     /// To close the soft keyboard 
     /// </summary> 
     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(); 
}