UWP 在检测按下 "Enter" 键时出现问题

UWP problems in detecting the "Enter" key pressed

当按下键盘上的 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")) + "%";
        }
    }

像这样将 KeyDown 事件附加到您的 TexBox :

<TextBox KeyDown="Box_KeyDown" />

在后端 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")) + "%";

    }
}

you were trying to check the KeyStatus which is not required in your usecase, instead you should be checking which key is pressed.