【发布时间】:2016-11-21 11:55:14
【问题描述】:
我正在尝试使用 XAML 和 VB 构建通用 Windows 应用程序(在 Visual Studio 2015 中)。任务是:一个简单的 TextBlock 应该显示使用 KeyDown 事件在键盘上键入的每个数字。 TextBlock 位于 MainPage 的 Grid 上。
我已经尝试过使用 Grid 的 KeyDown 事件和 MainPage 的 KeyDown 事件。它们似乎都不起作用(参见下面的代码)。按下一个键就不会触发事件。在 TextBox(显示当前按下的键的 TextBlock)中键入文本时,它工作得很好。但这不是我想要的。
XAML:
<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" KeyDown="Page_KeyDown">
<Grid x:Name="frm_main" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock x:Name="txt_Ausgabe" HorizontalAlignment="Left" Margin="704,118,0,210" TextWrapping="Wrap" Text="hgbv,hjghj" VerticalAlignment="Top" Height="752" Width="448" FontSize="72" TextAlignment="Center" FontWeight="Bold"/>
</Grid>
VB:
Public NotInheritable Class MainPage
Inherits Page
Private Sub Page_KeyDown(sender As Object, e As KeyRoutedEventArgs)
txt_Ausgabe.Text = e.Key
End Sub
End Class
【问题讨论】:
-
你需要实现的类是
CoreWindow.GetForCurrentThread类并绑定keydown事件。但是,我并不精通如何在 VB 中做到这一点(在 c# 中就像CoreWindow.GetForCurrentThread().KeyDown += Page_KeyDown一样简单) -
谢谢 Takarii,这正是我需要的提示!