IWeakEventListener.ReceiveWeakEvent(Type, Object, EventArgs) 方法


從集中式事件管理員接收事件。Receives events from the centralized event manager.

 bool ReceiveWeakEvent(Type ^ managerType, System::Object ^ sender, EventArgs ^ e);
public bool ReceiveWeakEvent (Type managerType, object sender, EventArgs e);
abstract member ReceiveWeakEvent : Type * obj * EventArgs -> bool
Public Function ReceiveWeakEvent (managerType As Type, sender As Object, e As EventArgs) As Boolean



呼叫這個方法的 WeakEventManager 型別。The type of the WeakEventManager calling this method.


發出事件的物件。Object that originated the event.


事件資料。Event data.


如果接聽程式已處理事件則為 truetrue if the listener handled the event. WPFWPF 中處理的 WeakEventManager 會將其視為錯誤,以註冊接聽程式未處理之事件的接聽程式。It is considered an error by the WeakEventManager handling in WPFWPF to register a listener for an event that the listener does not handle. 如果方法接收到無法辨識或處理的事件,不論如何都該傳回 falseRegardless, the method should return false if it receives an event that it does not recognize or handle.


下列範例ReceiveWeakEvent會執行, 以提供兩個假設性事件ClockwiseSpin的 WeakEvent CounterclockwiseSpin模式支援, 而且每WeakEventManager個都有專用的 (未顯示)。The following example implements ReceiveWeakEvent to provide WeakEvent pattern support for two hypothetical events ClockwiseSpin and CounterclockwiseSpin that each have a dedicated WeakEventManager (not shown). 此實作為會針對每個支援的事件呼叫類別處理常式存根, 並轉換事件資料 (在此案例中, 兩SpinEventArgs個事件會共用事件資料類型)。This implementation calls class handler stubs for each supported event, casting the event data (in this case the two events share an event data type SpinEventArgs). false如果收到的事件不是預期的事件, 則會傳回。The implementation returns false if the event received is not one that is expected.

bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
    if (managerType == typeof(ClockwiseSpinEventManager))
        OnClockwiseSpin(sender, (SpinEventArgs)e);
    else if (managerType == typeof(CounterclockwiseSpinEventManager))
        OnCounterclockwiseSpin(sender, (SpinEventArgs)e);
        return false;       // unrecognized event
    return true;
private void OnClockwiseSpin(object sender, SpinEventArgs e) {
    //do something here...
private void OnCounterclockwiseSpin(object sender, SpinEventArgs e) {
    //do something here...
Private Function ReceiveWeakEvent(ByVal managerType As Type, ByVal sender As Object, ByVal e As EventArgs) As Boolean Implements IWeakEventListener.ReceiveWeakEvent
    If managerType Is GetType(ClockwiseSpinEventManager) Then
        OnClockwiseSpin(sender, CType(e, SpinEventArgs))
    ElseIf managerType Is GetType(CounterclockwiseSpinEventManager) Then
        OnCounterclockwiseSpin(sender, CType(e, SpinEventArgs))
        Return False ' unrecognized event
    End If
    Return True
End Function
Private Sub OnClockwiseSpin(ByVal sender As Object, ByVal e As SpinEventArgs)
    'do something here...
End Sub
Private Sub OnCounterclockwiseSpin(ByVal sender As Object, ByVal e As SpinEventArgs)
    'do something here...
End Sub


這個方法的目的是要接收接收者在接聽程式清單中的所有可能事件。This method is intended to receive all possible events where the receiver is in a listener list. 特別是針對接聽一個以上事件的類別執行時, 您必須檢查傳入managerType參數的類型, 以知道呼叫對應的事件。Particularly if implementing for a class that is listening for more than one event, you must check the type of the incoming managerType parameter in order to know which event the call corresponds to. 這就是為什麼每個屬於模式的事件都必須具有專用WeakEventManager的原因。This is why each event that is to be part of the pattern must have a dedicated WeakEventManager. 此外, 參數e的類型為一般。 EventArgsAlso, the e parameter is typed as the general EventArgs. 因此, 若要從事件資料中取得任何特定的屬性, 您的執行必須轉換成特定的事件資料類型。Therefore, in order get any specific properties out of the event data, your implementation will need to cast to the specific event data type.

通常, 在根據WeakEventManager型別檢查事件的識別之後, 您的實作為應該呼叫私用類別處理常式。Typically, after checking for the identity of the event based on the WeakEventManager type, your implementation should call a private class handler. 您的類別處理常式可能是非常相同的類別處理常式, 可用於處理透過傳統事件模式新增+=至傳送者之事件的接聽程式事件。Your class handler could conceivably be the very same class handler that is used to handle that event for listeners that are added through the conventional event pattern of adding via += to the event on the sender. 下列範例區段提供粗略的執行範本。The following example section provides a rough implementation template.