.NET MAUI Windows平台如何添加全局键盘与鼠标事件监听器?
MAUI Windows平台实现用户无活动超时功能
在MAUI中,Windows平台没有直接继承WindowsPage的入口,但可以通过获取WinUI的Window实例,给根内容元素注册输入事件来实现会话延长逻辑,替代Xamarin UWP中的页面重写方式。
实现步骤
- 打开MAUI项目的Platforms/Windows/App.xaml.cs文件,在
OnLaunched方法中添加事件注册逻辑:
protected override void OnLaunched(LaunchActivatedEventArgs args) { base.OnLaunched(args); // 获取MAUI对应的WinUI窗口实例 var mauiWindow = Microsoft.Maui.Controls.Application.Current.Windows.FirstOrDefault(); var winuiWindow = mauiWindow?.Handler?.NativeView as Microsoft.UI.Xaml.Window; if (winuiWindow != null) { // 监听窗口内容变更,确保动态替换根元素时事件不会失效 winuiWindow.ContentChanged += OnWindowContentChanged; // 初始化注册当前根元素的事件 RegisterInputEvents(winuiWindow.Content as Microsoft.UI.Xaml.FrameworkElement); } } // 窗口内容变更时重新注册事件 private void OnWindowContentChanged(Microsoft.UI.Xaml.Window sender, Microsoft.UI.Xaml.WindowContentChangedEventArgs args) { RegisterInputEvents(sender.Content as Microsoft.UI.Xaml.FrameworkElement); } // 统一注册所有需要的输入事件 private void RegisterInputEvents(Microsoft.UI.Xaml.FrameworkElement rootElement) { if (rootElement == null) return; // 先移除原有事件绑定(避免重复注册) rootElement.CharacterReceived -= OnCharacterReceived; rootElement.DoubleTapped -= OnDoubleTapped; rootElement.KeyDown -= OnKeyDown; rootElement.KeyUp -= OnKeyUp; rootElement.Tapped -= OnTapped; rootElement.PointerMoved -= OnPointerMoved; rootElement.PointerPressed -= OnPointerPressed; rootElement.PointerReleased -= OnPointerReleased; // 重新注册事件 rootElement.CharacterReceived += OnCharacterReceived; rootElement.DoubleTapped += OnDoubleTapped; rootElement.KeyDown += OnKeyDown; rootElement.KeyUp += OnKeyUp; rootElement.Tapped += OnTapped; rootElement.PointerMoved += OnPointerMoved; rootElement.PointerPressed += OnPointerPressed; rootElement.PointerReleased += OnPointerReleased; } // 各事件触发时调用会话延长方法 private void OnCharacterReceived(object sender, Microsoft.UI.Xaml.Input.CharacterReceivedRoutedEventArgs e) { PageService.Instance.ExtendSession(); } private void OnDoubleTapped(object sender, Microsoft.UI.Xaml.Input.DoubleTappedRoutedEventArgs e) { PageService.Instance.ExtendSession(); } private void OnKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) { PageService.Instance.ExtendSession(); } private void OnKeyUp(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) { PageService.Instance.ExtendSession(); } private void OnTapped(object sender, Microsoft.UI.Xaml.Input.TappedRoutedEventArgs e) { PageService.Instance.ExtendSession(); } private void OnPointerMoved(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e) { PageService.Instance.ExtendSession(); } private void OnPointerPressed(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e) { PageService.Instance.ExtendSession(); } private void OnPointerReleased(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e) { PageService.Instance.ExtendSession(); }
注意事项
- 确保
PageService是你项目中已有的服务实例(比如单例),用于处理会话延长的核心逻辑,和你Xamarin项目中的实现保持一致即可。 - 监听
Window.ContentChanged事件是为了应对应用中动态替换根内容的场景(比如导航到新页面框架),避免事件监听失效。 - 事件注册前先移除原有绑定,防止同一事件被多次触发。
内容的提问来源于stack exchange,提问作者Rand Lennox
相关产品推荐
相关产品推荐

