EventManager.RegisterClassHandler EventManager.RegisterClassHandler EventManager.RegisterClassHandler Method

定義

註冊特殊路由事件的類別處理常式。Registers a class handler for a particular routed event.

多載

RegisterClassHandler(Type, RoutedEvent, Delegate) RegisterClassHandler(Type, RoutedEvent, Delegate) RegisterClassHandler(Type, RoutedEvent, Delegate)

註冊特殊路由事件的類別處理常式。Registers a class handler for a particular routed event.

RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean) RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean) 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) RegisterClassHandler(Type, RoutedEvent, Delegate) 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

參數

classType
Type Type Type

正要宣告類別處理之類別的型別。The type of the class that is declaring class handling.

routedEvent
RoutedEvent RoutedEvent RoutedEvent

要處理之事件的路由事件識別項。The routed event identifier of the event to handle.

handler
Delegate Delegate Delegate

對類別處理常式實作的參考。A reference to the class handler implementation.

範例

下列範例會加入的處理常式PreviewMouseLeftButtonDown, 並RegisterClassHandler呼叫。The 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

備註

類別處理是可用於路由事件的功能, 包括使用路由事件支援所實作為附加事件。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. 藉由在繼承基類的類別中覆寫這些方法, 您可以執行類別處理, RegisterClassHandler而不需要在靜態的函式中呼叫。By 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將設定為true, 來註冊要叫用的類別處理常式。You 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) RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean) 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

參數

classType
Type Type Type

正要宣告類別處理之類別的型別。The type of the class that is declaring class handling.

routedEvent
RoutedEvent RoutedEvent RoutedEvent

要處理之事件的路由事件識別項。The routed event identifier of the event to handle.

handler
Delegate Delegate Delegate

對類別處理常式實作的參考。A reference to the class handler implementation.

handledEventsToo
Boolean Boolean Boolean

true 表示即使路由事件的引數已標示為已處理,也要叫用這個類別處理常式,false 則表示保留預設行為,即不在任何標示為已處理的事件上叫用處理常式。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.

備註

類別處理是可用於路由事件的功能, 包括使用路由事件支援所實作為附加事件。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. 藉由在繼承基類的類別中覆寫這些方法, 您可以執行類別處理, RegisterClassHandler而不需要在靜態的函式中呼叫。By 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.

適用於