当按下键盘上的按键时,我尝试在我的应用程序中运行某些功能Enter,但在执行此操作时遇到问题。
KeyboardControl在KeyDown我的文本框中。
Key.Enter不被识别为函数,我不知道该怎么办。
// When a key is pressed on the keyboard
private void KeyboardControl(object sender, KeyEventArgs e)
{
if (e.KeyStatus == Key.Enter)
{
PercentCalc();
PercentageValue.Text = Convert.ToString(result, new CultureInfo("en-US")) + "%";
}
}
Run Code Online (Sandbox Code Playgroud)
将KeyDown事件附加到您的TexBox ,如下所示:
<TextBox KeyDown="Box_KeyDown" />
Run Code Online (Sandbox Code Playgroud)
在后端 keydown 事件中检查按下的键是否为Enter,然后在该 if 条件下执行代码。
private async void Box_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{//execute code here
PercentCalc();
PercentageValue.Text = Convert.ToString(result, new CultureInfo("en-US")) + "%";
}
}
Run Code Online (Sandbox Code Playgroud)
您试图检查用例中不需要的KeyStatus ,而是应该检查按下了哪个键。