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自訂路由事件的事件存取模式, 提供 "add" 存取子的執行。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 true將已定義的處理常式附加至頁面上的其中一個已命名專案的頁面上, 執行在事件上叫用的處理常式。The 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. 這適用于以handledEventsTootrue依據來註冊處理常式的這些案例。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自訂路由事件的事件存取模式, 提供 "add" 存取子的執行。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.

適用於