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) 與自訂路由事件相關聯的「包裝函式」事件,尤其是當您針對層級的處理常式執行「移除」邏輯時 CLRCLRThe 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.

routedEvent也不 handler 可能 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.