c# - 为什么尝试在WPF/C#中注册所有事件时出现构建错误?

下面是我的窗口中的构造函数和事件处理程序。

    public MyWindow() {
        InitializeComponent();

        foreach (RoutedEvent routedEvent in EventManager.GetRoutedEvents())
        {
            EventManager.RegisterClassHandler(typeof(TestUnbuggerWindow), routedEvent, OnRoutedEvent, true);
        }
    }

    public void OnRoutedEvent(Object sender, RoutedEventArgs args)
    {
    }

我正在尝试预览所有事件,因为我正在学习WPF,我认为这可能会揭露一些未发生的事情。无论如何,我得到以下构建错误:
Error   1   The best overloaded method match for 'System.Windows.EventManager.RegisterClassHandler(System.Type, System.Windows.RoutedEvent, System.Delegate, bool)' has some invalid arguments
Error   2   Argument 3: cannot convert from 'method group' to 'System.Delegate'

我得到了从this website注册所有事件的循环。

我相信问题是由方法签名不匹配引起的,但是我不确定。有人可以帮我一下吗?

最佳答案

您需要显式使用处理程序

EventManager.RegisterClassHandler(typeof(TestUnbuggerWindow), 
     routedEvent, new RoutedEventHandler(OnRoutedEvent), true);

这是因为编译器需要知道所需的处理程序类型,并且由于签名采用Delegate类型,因此无法推断出您想要RoutedEventHandler

https://stackoverflow.com/questions/10730307/

相关文章:

visual-c++ - 升级项目:operator '=' is ambiguous and 'i

ant - 如何在不使用 IDE(Netbeans 等)的情况下使用外部库编译 java 文件?

visual-studio-2010 - MS VS 2010 srand()无法编译?

iphone - 在编译应用程序时遇到这样的错误

compilation - 在我的编译器中完全可以正常工作,但在将其提交到在线社区时会出现编译错误

xcode - 项目清理后,Cocos2d/box2d项目将无法编译

ruby-on-rails - SCSS编译错误

android - Maven不会因引入的编译错误而失败

scala - 为什么在 Scala 中压缩比 zip 快?

android - 使用PendingIntent的编译错误