ContentElement.AddHandler ContentElement.AddHandler ContentElement.AddHandler Method

定義

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

多載

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

參數

routedEvent
RoutedEvent RoutedEvent RoutedEvent

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

handler
Delegate Delegate 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) AddHandler(RoutedEvent, Delegate, Boolean) 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

參數

routedEvent
RoutedEvent RoutedEvent RoutedEvent

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

handler
Delegate Delegate Delegate

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

handledEventsToo
Boolean Boolean 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. 針對這些案例是該註冊處理常式handledEventsToo做為true適合。It 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標示為已處理的類別處理,您可以加入處理常式PreviewMouseLeftButtonDown改為。For 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.

適用於