如何在WindowsPhone/Windows 8.1上按Enter键时隐藏软键盘

本文关键字:Enter 隐藏 键盘 上按 WindowsPhone Windows | 更新日期: 2023-09-27 18:20:36

当编辑TextBox时按下回车键时,我想隐藏软键盘。这就是我目前在c#:中所拥有的

private void SearchBox_KeyUp(object sender, KeyRoutedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        if(e.Key == VirtualKey.Enter)
        {
            this.Focus(FocusState.Programmatic); // sending focus to Page to hide keyboard
        }
    }

如何在WindowsPhone/Windows 8.1上按Enter键时隐藏软键盘

尝试立即禁用然后启用TextBox。

if(e.Key == VirtualKey.Enter)
{
    textBox.IsEnabled = false;
    textBox.IsEnabled = true;
}
private void BobbinRunningLength_KeyUp(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == VirtualKey.Enter)
        {
            this.Focus(FocusState.Programmatic);
        }
    }

这对我很有效。

试试这个。。

private void SearchBox_KeyUp(object sender, KeyRoutedEventArgs e)
{        
    if (e.Key == VirtualKey.Enter)
    {
        this.Focus();
    }
}

这应该行得通。

启用和禁用TextBox对我不起作用。
我正在将其用于Windows Phone 8.1运行时应用程序:

private void TextBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
  if (e.Key == Windows.System.VirtualKey.Enter)
  {
    Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.IsInputEnabled = false;
    Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.IsInputEnabled = true;                
  }
}

我知道这个问题已经得到了解决,但我想分享我的解决方案,我在XAML页面中使用虚拟页面(带有可见/折叠的网格)时遇到了问题,当软键盘存在并且页面发生更改时,软键盘不会自动隐藏,显然是因为集中控件的父控件被折叠,这是一种奇怪且未记录的行为。我的解决方案是将焦点设置在要显示的下一页的一个元素上,这样操作系统就可以理解它并隐藏软键盘:

xamlButton.Focus(FocusState.Programmatic);
selectPage(1);

其中selectPage(int);通过索引设置页面的可见或折叠属性。

将焦点设置为按钮效果很好,因为按钮没有键盘输入。