gpt4 book ai didi

.net - 在 Windows 8 Metro 风格的 c# 应用程序上捕获 Enter 和空格键

转载 作者:行者123 更新时间:2023-12-02 03:56:26 27 4
gpt4 key购买 nike

我正在使用 C# 和 XAML,在 Windows 8 上开发一个 metro 风格的应用程序。我想在 keyup 事件上捕获回车按钮,但它适用于除回车键和空格键之外的所有键盘按钮。我的代码

XAML

<Button x:Name="btnName" KeyUp="KeyboardKey_Pressed">
</Button>

C# 源代码

 public void KeyboardKey_Pressed(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
......
}

我知道如果我使用 TextBox 而不是 Button,它会起作用,但我想使用按钮。我该怎么做?

最佳答案

这个有效:

using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;


namespace YourNamespace
{
public sealed class NiceButton: Button
{
public NiceButton()
: base()
{

}
protected override void OnKeyUp(KeyRoutedEventArgs e)
{
}
}
}

通过包含命名空间的 xmlns,在 XAML 中使用 NiceButton 而不是 Button。

关于.net - 在 Windows 8 Metro 风格的 c# 应用程序上捕获 Enter 和空格键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12389231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com