首页 文章

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

提问于
浏览
2

我想在编辑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
        }
    }

5 回答

  • 5

    尝试短暂禁用然后启用TextBox .

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

    这对我有用 .

  • 4

    试试这个..

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

    这应该工作 .

  • 4

    启用和禁用TextBox对我来说不起作用 .
    我正在将它用于Windows Phone 8.1 Runtime应用程序:

    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;                
      }
    }
    
  • 0

    我知道这已经得到了解答但是我想分享我的解决方案,我在XAML页面中使用虚拟页面(Visible / Collapsed网格)时出现问题,当软键盘存在且页面改变时软键盘不自动隐藏,显然是因为集中控制的父母被坍塌,奇怪和无证的行为 . 我的解决方案是将焦点放在要显示的下一页的元素上,以便操作系统理解它并隐藏软键盘:

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

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

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

相关问题