page_keyup事件无效 [英] page_keyup event not working
问题描述
你好,我有一个关于我想在我的apllication中使用的页面key_up事件的问题。
Hello i have a question about the page key_up event i want to use with my apllication.
当我使用下面的代码时,它无法正常工作。
When i use the following code it does'nt work.
Xmlns
<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"
KeyUp="Page_KeyUp">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" HorizontalAlignment="Center">
<TextBlock Name="Test" Text="Testen" FontSize="48" TextWrapping="Wrap" />
</Grid>
</Grid>
</Page>
c#
namespace App2
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Page_KeyUp(object sender, KeyRoutedEventArgs e)
{
Test.Text = "Yes";
switch (e.Key)
{
case VirtualKey.P: Test.Text = "P"; break;
}
}
}
}
但是当我向页面添加按钮时,它可以工作
but when i add a button to the page it works
<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"
KeyUp="Page_KeyUp">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" HorizontalAlignment="Center">
<TextBlock Name="Test" Text="Testen" FontSize="48" TextWrapping="Wrap" />
</Grid>
<Grid Grid.Row="1">
<Button Name="Test1"/>
</Grid>
</Grid>
</Page>
我是否必须向页面添加控件才能使用页面key_up事件?
do i have to add a control to the page in order to work with the page key_up event?
当我按下遥控器(数字小键盘)上的按键时,我不需要对我的应用程序上的控制文件块显示给我的用户。
I don't need controls on my apllication only textblock's to show something to my users when they press a key on my remote control (a numeric keypad).
我希望有人可以帮我这个!!
I hope someone can help me with this!!
来自荷兰的问候
Adrie Pols
Adrie Pols
推荐答案
Hello A.Pols,
Hello A.Pols,
UI中的控件生成
键盘事件仅在他们有输入焦点时。
Controls in your UI generate keyboard events only when they have input focus.
你可以尝试
@Juan Carlos Velez方法。下面是一个示例:
You can try @Juan Carlos Velez method. Here is a sample:
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
Test.Text = "Yes";
switch (args.VirtualKey)
{
case VirtualKey.P: Test.Text = "P"; break;
}
}
祝你好运,
Rita
这篇关于page_keyup事件无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!