UIElement.RemoveHandler(RoutedEvent, Delegate) 方法


从此元素中删除指定的路由事件处理程序。Removes the specified routed event handler from this element.

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



附加了处理程序的路由事件的标识符。The identifier of the routed event for which the handler is attached.


要从此元素的事件处理程序集合中删除的特定处理程序实现。The specific handler implementation to remove from the event handler collection on this element.



下面的示例使用 RemoveHandler 作为事件包装器定义的一部分。The following example uses RemoveHandler as part of an event wrapper definition.

public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
    "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));

// Provide CLR accessors for the event
public event RoutedEventHandler Tap
        add { AddHandler(TapEvent, value); } 
        remove { RemoveHandler(TapEvent, value); }
Public Shared ReadOnly TapEvent As RoutedEvent = EventManager.RegisterRoutedEvent("Tap", RoutingStrategy.Bubble, GetType(RoutedEventHandler), GetType(MyButtonSimple))

' Provide CLR accessors for the event
Public Custom Event Tap As RoutedEventHandler
    AddHandler(ByVal value As RoutedEventHandler)
        Me.AddHandler(TapEvent, value)
    End AddHandler

    RemoveHandler(ByVal value As RoutedEventHandler)
        Me.RemoveHandler(TapEvent, value)
    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As RoutedEventArgs)
    End RaiseEvent
End Event


使用此 API 的最常见方案是实现与自定义路由事件相关联的 公共语言运行时 (CLR)common language runtime (CLR) "包装" 事件,特别是在 CLRCLR 级别实现处理程序的 "删除" 逻辑时。The most common scenario for using this API is when you implement the 公共语言运行时 (CLR)common language runtime (CLR) "wrapper" event that is associated with a custom routed event, specifically when you implement the "remove" logic for handlers at the CLRCLR level. 此备注部分后面的示例演示了这种情况。The example that follows this remarks section illustrates this scenario.

如果没有向与方法调用的输入参数匹配的条件注册处理程序,则调用此方法将不起作用。Calling this method has no effect if there were no handlers registered with criteria that matches the input parameters for the method call.

如果附加了多个与条件相匹配的处理程序,则只会删除事件处理程序存储中的第一个处理程序。If more than one handler is attached that matched the criteria, only the first handler in the event handler store is removed. 此行为与 -= 运算符 CLRCLR 行为一致。This behavior is consistent with CLRCLR behavior of the -= operator.

routedEventhandler 均无法 nullNeither routedEvent nor handler may be null. 尝试将任一值作为 null 提供会引发异常。Attempting to provide either value as null will raise an exception.

此方法将忽略 handledEventsToo 参数信息,如果处理程序是第一次用允许处理已处理事件的 AddHandler(RoutedEvent, Delegate, Boolean) 签名添加的,则提供此方法。This method ignores the handledEventsToo parameter information, which is provided if the handler was first added with the AddHandler(RoutedEvent, Delegate, Boolean) signature that enables handling of already-handled events. 已删除任一类型的处理程序。Either type of handler is removed.