Class
WeakEventManager<T>

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

Definition

Namespace:Telerik.Core

Assembly:Telerik.WinUI.Controls.dll

Type Parameters:

T

The type name.

Syntax:

cs-api-definition
public abstract class WeakEventManager<T> where T : class

Inheritance: objectWeakEventManager<T>

Derived Classes: CanExecuteChangedEventManagerCollectionChangedEventManagerCurrentChangedEventManagerCurrentChangingEventManagerPropertyChangedEventManager

Constructors

WeakEventManager()

Initializes a new instance of the WeakEventManager class. Creates the base values when it is used as the initializer by the constructor of a derived class.

Declaration

cs-api-definition
protected WeakEventManager()

Methods

CheckForNull(object, string)

Helper method that throw ArgumentNullException if given source is null.

Declaration

cs-api-definition
protected static void CheckForNull(object source, string argumentName)

Parameters

source

object

The element that is check for null.

argumentName

string

The name that should be passed to ArgumentNullException constructor.

DeliverEvent(object, object)

Delivers the event being managed to each listener.

Declaration

cs-api-definition
protected void DeliverEvent(object sender, object args)

Parameters

sender

object

The object on which the event is being handled.

args

object

An EventArgs that contains the event data for the event to deliver.

Remarks

Call the DeliverEvent method from within the event handlers that are added or removed by the StartListening and StopListening implementations of subclasses. If you call the ProtectedAddListener method in your AddListener implementation of your class, the list of listeners receiving the event is kept in an underlying collection. (AddListener is not part of an interface or class contract. AddListener is the suggested name for the method of your manager class that calls ProtectedAddListener and adds weak event pattern listeners for the event.) ProtectedAddListener adds listeners to a single list. If your manager implementation maintains more than one list of listeners per event, do not use DeliverEvent or ProtectedAddListener. Instead, your implementation should create its own WeakEventManager.ListenerList instances, AddListener should add listeners to the appropriate list, and events should be delivered to the appropriate listener list by calling DeliverEventToList.

DeliverEventToList(object, object, ListenerList)

Delivers the event being managed to each listener in the provided list.

Declaration

cs-api-definition
protected void DeliverEventToList(object sender, object args, WeakEventManager<T>.ListenerList list)

Parameters

sender

object

The object on which the event is being handled.

args

object

An EventArgs that contains the event data.

list

WeakEventManager<T>.ListenerList

The provided ListenerList.

Remarks

This method is necessary if your manager implementation maintains separate listeners lists based on information that is captured in the event data. If you use this advanced technique, you must create and maintain the separate lists as part of your manager implementation, and you must provide a way to add listeners to a specific list. Your handler implementation that listens to the raw event must act upon the condition that you use to differentiate the lists, and deliver the event only to the appropriate list or lists.

ProtectedAddListener(T, IWeakEventListener)

Adds the provided listener to the provided source for the event being managed.

Declaration

cs-api-definition
protected void ProtectedAddListener(T source, IWeakEventListener listener)

Parameters

source

T

The source to attach listeners to.

listener

IWeakEventListener

The listening class (which must implement IWeakEventListener).

Remarks

Call this method within your manager class AddListener methods on WeakEventManager implementations. AddListener is the suggested name for the static method you define on your manager class to enable other classes to add a listener for your weak event pattern. AddListener should take two parameters: the source where the listener is attached, and the listener instance. For your AddListener implementation, call the ProtectedAddListener method on the current manager and pass the same two parameters. If the list of listeners was previously empty, ProtectedAddListener calls the StartListening method internally, which will call your specific StartListening override through polymorphism. ProtectedAddListener adds listeners to a single internal WeakEventManager.ListenerList per source. If your manager implementation maintains more than one list of listeners for each event-source combination, do not use ProtectedAddListener. Instead, your implementation should create its own WeakEventManager.ListenerList instances, AddListener should add listeners to the appropriate list, and events should be delivered to the appropriate listener list by calling the DeliverEventToList event instead of the DeliverEvent method.

ProtectedRemoveListener(T, IWeakEventListener)

Removes a previously added listener from the provided source.

Declaration

cs-api-definition
protected void ProtectedRemoveListener(T source, IWeakEventListener listener)

Parameters

source

T

The source to remove listeners from.

listener

IWeakEventListener

The listening class (which must implement IWeakEventListener).

Remarks

Call this method within your manager class RemoveListener methods on WeakEventManager implementations. RemoveListener is the suggested name for the static method you define on your manager class to enable other classes to remove a listener for your weak event pattern. RemoveListener should take two parameters: the source where the listener is removed, and the listener class. For your RemoveListener implementation, call the ProtectedRemoveListener method on the current manager and pass the same two parameters. If a call to ProtectedRemoveListener removes the last listener in the list, ProtectedRemoveListener calls the StopListening method internally, which will call your specific StopListening override through polymorphism. ProtectedRemoveListener removes listeners from a single internal WeakEventManager.ListenerList per source. If your manager implementation maintains more than one list of listeners for each event-source combination, do not use ProtectedRemoveListener. Instead, your implementation should create its own WeakEventManager.ListenerList instances, RemoveListener should remove listeners from the appropriate list, and events should be delivered to the appropriate listener list by calling the DeliverEventToList method instead of the DeliverEvent method.

StartListening(object)

When overridden in a derived class, starts listening for the event being managed. After the StartListening method is first called, the manager should be in the state of calling DeliverEvent or DeliverEventToList whenever the relevant event from the provided source is handled.

Declaration

cs-api-definition
protected abstract void StartListening(object source)

Parameters

source

object

The source to begin listening on.

Remarks

Notes to Inheritors StartListening overrides should add a handler to the provided source. The handler is declared by the manager itself. The class handler should not be public, and it should only be called in response to the event being managed. The class handler should call the DeliverEvent method or the DeliverEventToList method appropriately.

StopListening(object)

When overridden in a derived class, stops listening on the provided source for the event being managed.

Declaration

cs-api-definition
protected abstract void StopListening(object source)

Parameters

source

object

The source to stop listening on.

Remarks

Notes to Inheritors StopListening implementations should remove the class handler as added by the StartListening method. Removing a listener should not clear the listener list. Instead, it should only disconnect the class handler (perhaps temporarily).