UIElement.AddHandler UIElement.AddHandler UIElement.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. 这适用于将处理程序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事件访问模式提供 "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.

适用于