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



要附加處理常式之路由事件的識別項。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.