EventManager.RegisterClassHandler Method

Definition

为特定路由事件注册类处理程序。Registers a class handler for a particular routed event.

Overloads

RegisterClassHandler(Type, RoutedEvent, Delegate)

为特定路由事件注册类处理程序。Registers a class handler for a particular routed event.

RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean)

使用处理事件数据已标记为已处理的事件的选项,为特定路由事件注册类处理程序。Registers a class handler for a particular routed event, with the option to handle events where event data is already marked handled.

RegisterClassHandler(Type, RoutedEvent, Delegate)

为特定路由事件注册类处理程序。Registers a class handler for a particular routed event.

public:
 static void RegisterClassHandler(Type ^ classType, System::Windows::RoutedEvent ^ routedEvent, Delegate ^ handler);
public static void RegisterClassHandler (Type classType, System.Windows.RoutedEvent routedEvent, Delegate handler);
static member RegisterClassHandler : Type * System.Windows.RoutedEvent * Delegate -> unit
Public Shared Sub RegisterClassHandler (classType As Type, routedEvent As RoutedEvent, handler As Delegate)

Parameters

classType
Type

声明类处理的类的类型。The type of the class that is declaring class handling.

routedEvent
RoutedEvent

要处理的事件的路由事件标识符。The routed event identifier of the event to handle.

handler
Delegate

对类处理程序实现的引用。A reference to the class handler implementation.

Examples

下面的示例为 PreviewMouseLeftButtonDown添加了一个处理程序,调用了 RegisterClassHandlerThe following example adds a handler for PreviewMouseLeftButtonDown, calling RegisterClassHandler.

static MyEditContainer()
{
  EventManager.RegisterClassHandler(typeof(MyEditContainer), PreviewMouseRightButtonDownEvent, new RoutedEventHandler(LocalOnMouseRightButtonDown));
}
internal static void LocalOnMouseRightButtonDown(object sender, RoutedEventArgs e)
{
  MessageBox.Show("this is invoked before the On* class handler on UIElement");
  //e.Handled = true; //uncommenting this would cause ONLY the subclass' class handler to respond
}
Shared Sub New()
  EventManager.RegisterClassHandler(GetType(MyEditContainer), PreviewMouseRightButtonDownEvent, New RoutedEventHandler(AddressOf LocalOnMouseRightButtonDown))
End Sub
Friend Shared Sub LocalOnMouseRightButtonDown(ByVal sender As Object, ByVal e As RoutedEventArgs)
  MessageBox.Show("this is invoked before the On* class handler on UIElement")
  'e.Handled = True //uncommenting this would cause ONLY the subclass' class handler to respond
End Sub

Remarks

类处理是适用于路由事件的一项功能,其中包括使用路由事件支持实现的附加事件。Class handling is a feature that is available for routed events, including attached events that are implemented with routed event backing. 类处理程序就像是为类的所有实例都存在的静态处理程序。A class handler is like a static handler that exists for all instances of the class. 由于处理程序是静态的,因此不能使用类处理程序直接更改实例属性,但可以通过 sender 参数和/或事件数据访问实例。Because the handler is static, you cannot change instance properties directly with a class handler, but you can access instances through the sender parameter and/or the event data.

类处理程序在实例处理程序之前调用。Class handlers are invoked before instance handlers. 可以实现一个类处理程序,它具有将事件标记为已处理的行为。You can implement a class handler that has the behavior of marking the event as handled. 因此,不会调用类处理事件的实例处理程序,除非实例处理程序专门注册已处理事件。Therefore, instance handlers for a class-handled event are not invoked unless the instance handlers register specifically for handled events.

许多 WPFWPF 基元素事件提供类处理虚拟方法。Many of the WPFWPF base element events provide class handling virtual methods. 通过在继承基类的类中重写这些方法,您可以实现类处理,而无需在静态构造函数中调用 RegisterClassHandlerBy overriding these methods in classes that inherit the base classes, you can implement class handling without calling RegisterClassHandler in static constructors. 这些类处理方法通常存在于输入事件中,其名称以 "On" 开头,以正在处理类的事件名称结尾。These class handling methods typically exist for input events and have names that start with "On" and end with the name of the event being class handled.

有关类处理的详细信息,请参阅将路由事件标记为 "已处理" 和 "类处理"。For more information about class handling, see Marking Routed Events as Handled, and Class Handling.

使用此签名,类处理程序将注册为仅调用以响应未处理的事件。Using this signature, class handlers will be registered to invoke only in response to unhandled events. 你还可以注册类处理程序,以便在事件参数被标记为已处理时,使用 RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean) 签名,并将 handledEventsToo 设置为 trueYou can also register class handlers to invoke even if the event arguments are marked handled, by using the RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean) signature, with handledEventsToo set to true.

RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean)

使用处理事件数据已标记为已处理的事件的选项,为特定路由事件注册类处理程序。Registers a class handler for a particular routed event, with the option to handle events where event data is already marked handled.

public:
 static void RegisterClassHandler(Type ^ classType, System::Windows::RoutedEvent ^ routedEvent, Delegate ^ handler, bool handledEventsToo);
public static void RegisterClassHandler (Type classType, System.Windows.RoutedEvent routedEvent, Delegate handler, bool handledEventsToo);
static member RegisterClassHandler : Type * System.Windows.RoutedEvent * Delegate * bool -> unit
Public Shared Sub RegisterClassHandler (classType As Type, routedEvent As RoutedEvent, handler As Delegate, handledEventsToo As Boolean)

Parameters

classType
Type

声明类处理的类的类型。The type of the class that is declaring class handling.

routedEvent
RoutedEvent

要处理的事件的路由事件标识符。The routed event identifier of the event to handle.

handler
Delegate

对类处理程序实现的引用。A reference to the class handler implementation.

handledEventsToo
Boolean

如果即使已将路由事件的参数标记为已处理时也调用此类处理程序,则为 true;如果保留不对任何标记为已处理的事件调用处理程序的默认行为,则为 falsetrue 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.

Remarks

类处理是适用于路由事件的一项功能,其中包括使用路由事件支持实现的附加事件。Class handling is a feature that is available for routed events, including attached events that are implemented with routed event backing. 类处理程序就像是为类的所有实例都存在的静态处理程序。A class handler is like a static handler that exists for all instances of the class. 由于处理程序是静态的,因此不能使用类处理程序直接更改实例属性,但可以通过 sender 参数和/或事件数据访问实例。Because the handler is static, you cannot change instance properties directly with a class handler, but you can access instances through the sender parameter and/or the event data.

类处理程序在实例处理程序之前调用。Class handlers are invoked before instance handlers. 可以实现一个类处理程序,它具有将事件标记为已处理的行为。You can implement a class handler that has the behavior of marking the event as handled. 因此,不会调用类处理事件的实例处理程序,除非实例处理程序专门注册已处理事件。Therefore, instance handlers for a class-handled event are not invoked unless the instance handlers register specifically for handled events.

许多 WPFWPF 基元素事件提供类处理虚拟方法。Many of the WPFWPF base element events provide class handling virtual methods. 通过在继承基类的类中重写这些方法,您可以实现类处理,而无需在静态构造函数中调用 RegisterClassHandlerBy overriding these methods in classes that inherit the base classes, you can implement class handling without calling RegisterClassHandler in static constructors. 这些类处理方法通常存在于输入事件中,其名称以 "On" 开头,以正在处理类的事件名称结尾。These class handling methods typically exist for input events and have names that start with "On" and end with the name of the event being class handled.

有关类处理的详细信息,请参阅将路由事件标记为 "已处理" 和 "类处理"。For more information about class handling, see Marking Routed Events as Handled, and Class Handling.

使用此签名,可以通过将 handledEventsToo 设置为 true来注册类处理程序,以便在处理的事件上调用。Using this signature, class handlers can be registered to invoke upon handled events, by setting handledEventsToo set to true. 通常,仅当存在您尝试解决的已知处理问题(例如,通过鼠标或键盘事件进行输入系统处理)时,才应执行此操作。Generally, you should do this only if there is a known handling issue you are trying to work around, such as input system handling from mouse or keyboard events.

Applies to