संपादित करें

इसके माध्यम से साझा किया गया


UIElement.AddHandler Method

Definition

Adds a routed event handler for a specified routed event, adding the handler to the handler collection on the current element.

Overloads

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. 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)

Parameters

routedEvent
RoutedEvent

An identifier for the routed event to be handled.

handler
Delegate

A reference to the handler implementation.

Implements

Remarks

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.

You typically use this method to provide the implementation of the "add" accessor for the Microsoft .NET event access pattern of a custom routed event.

Applies to

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. 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)

Parameters

routedEvent
RoutedEvent

An identifier for the routed event to be handled.

handler
Delegate

A reference to the handler implementation.

handledEventsToo
Boolean

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.

The default is false.

Do not routinely ask to rehandle a routed event.

Examples

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

Remarks

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. 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. 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.

You typically use this method to provide the implementation of the "add" accessor for the Microsoft .NET event access pattern of a custom routed event.

Applies to