Is there a way to watch WPF Routed Events?

前端 未结 2 1633
不思量自难忘°
不思量自难忘° 2021-02-02 14:49

I was wondering if there\'s a way to watch all RoutedEvents that are raised in a WPF application. A way to write some info about the events fired to the console would be prefect

2条回答
  •  醉话见心
    2021-02-02 14:55

    I've found another way:

    I've added this to the loaded handler of my UserControl.

    var events = EventManager.GetRoutedEvents();
    foreach (var routedEvent in events)
    {
        EventManager.RegisterClassHandler(typeof(myUserControl), 
                                          routedEvent, 
                                          new RoutedEventHandler(handler));
    }
    

    and this is the handler method:

    internal static void handler(object sender, RoutedEventArgs e)
    {
        if (e.RoutedEvent.ToString() != "CommandManager.PreviewCanExecute" &&
                e.RoutedEvent.ToString() != "CommandManager.CanExecute")
            Console.WriteLine(e.OriginalSource+"=>"+e.RoutedEvent);
    }
    

    The CanExecute events are a bit too much in my case. If you would like to see these too, just remove the if statement.

提交回复
热议问题