【问题标题】:How to Hide Keyboard in WinRT by Code如何通过代码在 WinRT 中隐藏键盘
【发布时间】:2012-12-16 03:22:47
【问题描述】:

在我的 winRT C# 应用程序中,我有一个文本框,当文本框被禁用时,可视键盘必须隐藏。但它没有按预期工作。

<Page
x:Class="App2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <TextBox HorizontalAlignment="Left" Margin="458,60,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="98" Width="389" KeyUp="TextBox_KeyUp_1"/>
</Grid>

C#

private void TextBox_KeyUp_1(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
                (sender as TextBox).IsEnabled = false;
        }

【问题讨论】:

  • “没有按预期工作”是什么意思?我使用上面的事件处理程序创建了一个 TextBox,当我点击 Enter 时,控件被禁用并且键盘关闭。
  • @DamirArh,但不适合我,键盘没有关闭

标签: c# xaml windows-runtime keyboard


【解决方案1】:

【讨论】:

  • 这个示例展示了当虚拟键盘出现时如何反应。我认为 vibeeshanRC 想要完全阻止它出现。
  • 乌尔是伊拉克的一座古城。请避免在 SO 帖子中使用文字和仅链接的答案。我们在这里有很高的标准;)
【解决方案2】:

尝试通过这个来关注另一个 UIElement:

    Descendents(MainFrame).OfType<Button>().FirstOrDefault().Focus(FocusState.Pointer);


    public static IEnumerable<DependencyObject> Descendents(DependencyObject root)
    {
        int count = VisualTreeHelper.GetChildrenCount(root);
        for (int i = 0; i < count; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(root, i);
            yield return child;
            foreach (DependencyObject descendent in Descendents(child))
                yield return descendent;
        }
    }

【讨论】:

    【解决方案3】:

    就我而言,我有一个包含几个 TextBox 字段的表单。在其中之一中,我不希望应用程序显示数字键盘。

    为了防止这种情况,我指定了属性 IsReadOnly="True"。

    我使用了 IsReadOnly 属性而不是 IsEnabled="False",因为 IsEnabled 禁用了 Tapped 事件,并且当我们在控件之间切换时它不会停止。

    <TextBox Name="tbx" Style="{StaticResource TextBoxDefaultStyle}" TabIndex="13" IsReadOnly="True" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      相关资源
      最近更新 更多