在MAUI中Content.KeyUp无法捕获按下Enter键

问题描述 投票:0回答:1

在MAUI中,使用KeyUp我可以捕获所有按键,但不能捕获Enter键。这是代码:

window.Content.KeyUp += (s,e){
   //do something
}

如果我按键盘的 Enter 键,什么也不会发生。如果我按其他键,KeyUp 处理程序将被触发。

是什么导致了这个问题?我该如何解决这个问题?有什么建议吗?

谢谢!

maui keypress enter keyup
1个回答
0
投票

你可以尝试下面的代码,它可以捕获按下回车键。

public partial class NewPage1 : ContentPage
{
    public NewPage1()
    {
        InitializeComponent();
    }

    protected override void OnHandlerChanged()
    {
        base.OnHandlerChanged();
#if WINDOWS
        Microsoft.UI.Xaml.Window window = (Microsoft.UI.Xaml.Window)App.Current.Windows.First<Window>().Handler.PlatformView;
        window.Content.KeyUp += Content_KeyUp;
#endif
    }
 
#if WINDOWS
    private void Content_KeyUp(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        var a = e.Key.ToString();
    }
#endif

}

这是效果

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.