UIElement.AddHandler 方法

定義

加入所指定路由事件的路由事件處理常式,會將此處理常式加入目前項目的處理常式集合中。Adds a routed event handler for a specified routed event, adding the handler to the handler collection on the current element.

多載

AddHandler(RoutedEvent, Delegate)

加入所指定路由事件的路由事件處理常式,會將此處理常式加入目前項目的處理常式集合中。Adds a routed event handler for a specified routed event, adding the handler to the handler collection on the current element.

AddHandler(RoutedEvent, Delegate, Boolean)

加入所指定路由事件的路由事件處理常式,會將此處理常式加入目前項目的處理常式集合中。Adds a routed event handler for a specified routed event, adding the handler to the handler collection on the current element. handledEventsToo 指定為 true,為已標示為由事件路由上另一個項目處理的事件叫用提供的處理常式。Specify handledEventsToo as true to have the provided handler be invoked for routed event that had already been marked as handled by another element along the event route.

AddHandler(RoutedEvent, Delegate)

加入所指定路由事件的路由事件處理常式,會將此處理常式加入目前項目的處理常式集合中。Adds a routed event handler for a specified routed event, adding the handler to the handler collection on the current element.

public:
 virtual void AddHandler(System::Windows::RoutedEvent ^ routedEvent, Delegate ^ handler);
public void AddHandler (System.Windows.RoutedEvent routedEvent, Delegate handler);
abstract member AddHandler : System.Windows.RoutedEvent * Delegate -> unit
override this.AddHandler : System.Windows.RoutedEvent * Delegate -> unit
Public Sub AddHandler (routedEvent As RoutedEvent, handler As Delegate)

參數

routedEvent
RoutedEvent

要處理的路由事件識別項。An identifier for the routed event to be handled.

handler
Delegate

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

實作

備註

您可以針對相同的事件多次新增相同的處理常式,而不會引發例外狀況。You can add the same handler for the same event multiple times without raising an exception. 不過,處理事件時,實際上會多次叫用處理程式。However, the handler is actually invoked multiple times when the event is handled. 因此,請考慮這種行為可能會對處理常式的執行產生副作用。Therefore, consider how this behavior might have side effects that should be accounted for in your handler implementation.

您通常會使用這個方法,為 Microsoft .NETMicrosoft .NET 自訂路由事件的事件存取模式,提供「加入」存取子的執行。You typically use this method to provide the implementation of the "add" accessor for the Microsoft .NETMicrosoft .NET event access pattern of a custom routed event.

適用於

AddHandler(RoutedEvent, Delegate, Boolean)

加入所指定路由事件的路由事件處理常式,會將此處理常式加入目前項目的處理常式集合中。Adds a routed event handler for a specified routed event, adding the handler to the handler collection on the current element. handledEventsToo 指定為 true,為已標示為由事件路由上另一個項目處理的事件叫用提供的處理常式。Specify handledEventsToo as true to have the provided handler be invoked for routed event that had already been marked as handled by another element along the event route.

public:
 void AddHandler(System::Windows::RoutedEvent ^ routedEvent, Delegate ^ handler, bool handledEventsToo);
public void AddHandler (System.Windows.RoutedEvent routedEvent, Delegate handler, bool handledEventsToo);
member this.AddHandler : System.Windows.RoutedEvent * Delegate * bool -> unit
Public Sub AddHandler (routedEvent As RoutedEvent, handler As Delegate, handledEventsToo As Boolean)

參數

routedEvent
RoutedEvent

要處理的路由事件識別項。An identifier for the routed event to be handled.

handler
Delegate

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

handledEventsToo
Boolean

true 登錄處理常式,使得即使路由事件被標記為已處理時,也會被叫用。false 登錄處理常式,預設條件為若路由事件被標示為已處理時則不被叫用。true to register the handler such that it is invoked even when the routed event is marked handled in its event data; false to register the handler with the default condition that it will not be invoked if the routed event is already marked handled.

預設為 falseThe default is false.

請勿定期要求重新處理路由事件。Do not routinely ask to rehandle a routed event.

範例

下列範例 Initialized 會使用將已定義的處理常式附加至頁面上的其中一個已命名元素的頁面上,執行在事件上叫用的處理常式 handledEventsToo trueThe following example implements a handler invoked on the Initialized event on a page that attaches a defined handler to one of the named elements on the page using handledEventsToo true. 即使路由上的另一個元素將共用事件資料標示為已處理,然後才到達路由中的處理專案,也會叫用這個處理常式。This handler would be invoked even if another element along the route marked the shared event data as handled before reaching the handling element in the route.

void PrimeHandledToo(object sender, EventArgs e)
{
    dpanel2.AddHandler(Button.ClickEvent, new RoutedEventHandler(GetHandledToo), true);
}
Private Sub PrimeHandledToo(ByVal sender As Object, ByVal e As EventArgs)
    dpanel2.AddHandler(Button.ClickEvent, New RoutedEventHandler(AddressOf GetHandledToo), True)
End Sub

備註

以實用的方式處理低層級的輸入事件是一項複雜的工作。Processing low-level input events in a practical way is a complex task. 許多控制項都是在特定事件標示為已處理的情況下執行行為,並以另一個更直覺的事件來取代。Many controls implement behavior where a certain event is marked as handled, and is replaced by another more intuitive event. 一般而言,控制項只會將平臺輸入事件標示為已處理(如果有一些設計意圖)。Generally, a control will only mark a platform input event as handled if there is some design intention for doing so. 在某些情況下,這些設計意圖可能不是您特定的輸入事件處理所需的內容。In certain scenarios, those design intentions might not be what your particular handling of the input event requires. 這適用于以 as 註冊處理常式的 handledEventsToo 案例 trueIt is for these scenarios that registering handlers with handledEventsToo as true is appropriate. 但是您不應該定期進行。But you should not do this routinely. 即使已處理,也會叫用處理程式來回應所有事件,以使您的應用程式事件處理邏輯變得複雜。Invoking handlers in response to all events even if handled will complicate your own application event processing logic. 如果處理常式邏輯很明顯,您可能會看到效能降低。You may see a decrease in performance if the handler logic is substantial. 如果您在開發過程中發現某些控制項處理的事件是您仍然想要使用應用程式邏輯處理的,您應該針對已處理的事件保留使用附加處理常式的情況。You should reserve the use of attaching handlers for already-handled events for situations where you have already discovered during the development process that certain controls are handling events that you still want to handle with application logic.

避免特定事件控制群組合之類別處理行為的另一種方法,是使用該事件的預覽替代方案。Another technique for avoiding the class handling behavior of certain event-control combinations is to use that event's preview alternative. 例如,如果 MouseLeftButtonDown 標示為由類別處理所處理,您可能可以改為加入的處理常式 PreviewMouseLeftButtonDownFor example, if MouseLeftButtonDown is marked handled by class handling, you might be able to add handlers for PreviewMouseLeftButtonDown instead.

您可以針對相同的事件多次新增相同的處理常式,而不會引發例外狀況。You can add the same handler for the same event multiple times without raising an exception. 不過,處理事件時,實際上會多次叫用處理程式。However, the handler is actually invoked multiple times when the event is handled. 因此,請考慮這種行為可能會對處理常式的執行產生副作用。Therefore, consider how this behavior might have side effects that should be accounted for in your handler implementation.

您通常會使用這個方法,為 Microsoft .NETMicrosoft .NET 自訂路由事件的事件存取模式,提供「加入」存取子的執行。You typically use this method to provide the implementation of the "add" accessor for the Microsoft .NETMicrosoft .NET event access pattern of a custom routed event.

適用於