Export (0) Print
Expand All

IWeakEventListener.ReceiveWeakEvent Method

Receives events from the centralized event manager.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)

bool ReceiveWeakEvent(
	Type managerType,
	Object sender,
	EventArgs e
)

Parameters

managerType
Type: System.Type
The type of the WeakEventManager calling this method.
sender
Type: System.Object
Object that originated the event.
e
Type: System.EventArgs
Event data.

Return Value

Type: 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 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...
}


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft