System.Windows Namespace


.NET Framework Class Library
WeakEventManager Class

Provides a base class for the event manager that is used in the WeakEvent pattern. The manager adds and removes listeners for events (or callbacks) that also use the pattern.

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

Visual Basic (Declaration)
Public MustInherit Class WeakEventManager _
    Inherits DispatcherObject
Visual Basic (Usage)
Dim instance As WeakEventManager
C#
public abstract class WeakEventManager : DispatcherObject
Visual C++
public ref class WeakEventManager abstract : public DispatcherObject
JScript
public abstract class WeakEventManager extends DispatcherObject
XAML
This class is abstract; see Inheritance Hierarchy for derived non-abstract classes usable in XAML.
Remarks

The principal reason for following the WeakEvent pattern is when the event source has an object lifetime that is potentially independent of the event listeners. Using the central event dispatching of a WeakEventManager allows the listener's handlers to be garbage collected even if the source object persists. By contrast, a normal event hookup using += causes the potentially disconnected source to hold a reference to the listeners, thus keeping the receiver from being collected in a timely fashion.

One common situation where the lifetime relationships between sources and listeners suggests the use of this pattern is the handling of update events coming from sources for data bindings.

The pattern can also be used for callbacks as well as for true events.

Notes to Inheritors:

Classes that derive from WeakEventManager class should do the following:

  • Provide a static "AddListener" method. Sources call AddListener to add a listener for the managed weak event. Your implementation calls ProtectedAddListener to implement this behavior.

  • Provide a static "RemoveListener" method. Sources call RemoveListener to add a listener for the managed event. Your implementation calls ProtectedRemoveListener to implement this behavior.

  • Override StartListening to cast the source to the type that owns the event, and connect the handler on the source to the event being managed.

  • Override StopListening to cast the source to the type that owns the event, and disconnect the handler on the source to the event being managed.

  • Implement the handler, which should call DeliverEvent, so that the managed event is forwarded to its weak event pattern listeners.

  • Provide a CurrentManager property that returns the specific manager type being implemented. The get accessor for CurrentManager should call GetCurrentManager to make sure that there is not already an initialized instance. If so, that instance is returned, properly typed. If there is no initialized instance, the get accessor should call SetCurrentManager to initialize one.

Inheritance Hierarchy

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.WeakEventManager
      System.Collections.Specialized..::.CollectionChangedEventManager
      System.ComponentModel..::.CurrentChangedEventManager
      System.ComponentModel..::.CurrentChangingEventManager
      System.ComponentModel..::.PropertyChangedEventManager
      System.Windows.Data..::.DataChangedEventManager
      System.Windows..::.LostFocusEventManager
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Page view tracker