IWeakEventListener.ReceiveWeakEvent Method (Type, Object, EventArgs)
Receives events from the centralized event manager.
Assembly: WindowsBase (in WindowsBase.dll)
Function ReceiveWeakEvent ( managerType As Type, sender As Object, e As EventArgs ) As Boolean
Object that originated the event.
Return ValueType: System.Boolean
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.
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.
The following example implements 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.to provide WeakEvent pattern support for two hypothetical events
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
Available since 3.0