如何在WPF中将evenHandler设置为所有窗口(整个应用程序)? [英] How to set an evenHandler in WPF to all windows (entire application)?

查看:107
本文介绍了如何在WPF中将evenHandler设置为所有窗口(整个应用程序)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个事件处理程序(例如 keydown )设置为整个解决方案,而不是单个窗口?

How can I set an event handler (such as keydown) to entire solution, not a single window?

推荐答案

在应用程序类(App.cs)中注册全局事件处理程序,如下所示:

Register a global event handler in your application class (App.cs), like this:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        EventManager.RegisterClassHandler(typeof(Window), Window.KeyDownEvent, new RoutedEventHandler(Window_KeyDown));
    }

    void Window_KeyDown(object sender, RoutedEventArgs e)
    {
        // your code here
    }
}

这将处理任何<$ c $的 KeyDown 事件c>窗口在您的应用程序您可以将 e 转换为 KeyEventArgs 以获取有关按键的信息。

This will handle the KeyDown event for any Window in your app. You can cast e to KeyEventArgs to get to the information about the pressed key.

这篇关于如何在WPF中将evenHandler设置为所有窗口(整个应用程序)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆