/// <summary> /// Registers a class handler for a particular routed event, with the option to handle events where event data is already marked handled. /// </summary> /// <param name="classType">The type of the class that is declaring class handling.</param> /// <param name="routedEvent">The routed event identifier of the event to handle.</param> /// <param name="handler">A reference to the class handler implementation.</param> /// <param name="handledEventsToo">true to invoke this class handler even if arguments of the routed event have been marked as handled; /// false to retain the default behavior of not invoking the handler on any marked-handled event.</param> /// <exception cref="ArgumentNullException"><paramref name="classType"/>, <paramref name="routedEvent"/>, or <paramref name="handler"/> is null.</exception> public static void RegisterClassHandler <T>(Type classType, RoutedEvent <T> routedEvent, EventHandler <T> handler, bool handledEventsToo = false) where T : RoutedEventArgs { if (classType == null) { throw new ArgumentNullException(nameof(classType)); } if (routedEvent == null) { throw new ArgumentNullException(nameof(routedEvent)); } if (handler == null) { throw new ArgumentNullException(nameof(handler)); } lock (SyncRoot) { if (!ClassesToClassHandlers.ContainsKey(classType)) { ClassesToClassHandlers[classType] = new Dictionary <RoutedEvent, RoutedEventHandlerInfo>(); } ClassesToClassHandlers[classType][routedEvent] = new RoutedEventHandlerInfo <T>(handler, handledEventsToo); } }
/// <summary> /// Get the class handler for the class <paramref name="classType"/> and routed event <paramref name="routedEvent"/>. /// </summary> /// <param name="classType">The type of the class that is handling the event.</param> /// <param name="routedEvent">The routed event to handle</param> /// <returns>The class handler</returns> /// <exception cref="ArgumentNullException"><paramref name="classType"/>, or <paramref name="routedEvent"/> is null.</exception> internal static RoutedEventHandlerInfo GetClassHandler(Type classType, RoutedEvent routedEvent) { if (classType == null) { throw new ArgumentNullException(nameof(classType)); } if (routedEvent == null) { throw new ArgumentNullException(nameof(routedEvent)); } var currentType = classType; while (currentType != null) { if (ClassesToClassHandlers.ContainsKey(currentType) && ClassesToClassHandlers[currentType].ContainsKey(routedEvent)) { return(ClassesToClassHandlers[currentType][routedEvent]); } currentType = currentType.GetTypeInfo().BaseType; } return(null); }