xamarin 表单上的按键未输入

Keypress on xamarin form not entry

我有一个 Xamarin Android 表单(内容页面),我希望当用户按下某个键盘键时执行某个操作,例如调用方法。我无法在任何地方找到任何解决方案。 如果有人知道如何做到这一点,我将非常感谢 his/her 帮助。

谢谢

尝试在您的 MainActivity

中覆盖 DispatchKeyEvent
    public override bool DispatchKeyEvent(KeyEvent e)
    {
        // if the Keycode is A .. there's lots of these
        //you'll have to checkout the Keycode class for more options
        //this example will fire the following code when 'A' is pressed
        if (e.KeyCode == Keycode.A)
        {
            //invoke your method here
        } 

        //return base if needed, otherwise keystrokes won't get automatically passed into textboxes etc and stuff will act wacky
        return base.DispatchKeyEvent(e);
    }

https://docs.microsoft.com/en-us/dotnet/api/android.app.activity.dispatchkeyevent?view=xamarin-android-sdk-9

如果这不起作用,您还可以覆盖 OnKeyDownOnKeyUp 以及 OnKey AFAIK

Difference between onKey(), OnKeyDown() and dispatchKeyEvent() methods provided by Android?