แก้ไข

แชร์ผ่าน


IWeakEventListener.ReceiveWeakEvent(Type, Object, EventArgs) Method

Definition

Receives events from the centralized event manager.

public:
 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

Parameters

managerType
Type

The type of the WeakEventManager calling this method.

sender
Object

Object that originated the event.

e
EventArgs

Event data.

Returns

true if the listener handled the event. It is considered an error by the WeakEventManager handling in WPF to register a listener for an event that the listener does not handle. Regardless, the method should return false if it receives an event that it does not recognize or handle.

Examples

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). 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). 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);
    }
    else
    {
        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))
    Else
        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

Notes to Implementers

This method is intended to receive all possible events where the receiver is in a listener list. 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. This is why each event that is to be part of the pattern must have a dedicated WeakEventManager. Also, 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.

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.

Applies to

See also