In This Article

WeakEventListener<TInstance, TEventArgs> Class

Provides a listener that allows for "weak" listening to events.

public class WeakEventListener<TInstance, TEventArgs> where TInstance : class where TEventArgs : EventArgs
Type Parameters:
TInstance -

The instance type that owns the listener.

TEventArgs -

The event arguments type.

Inheritance:
object object

Remarks

Make sure that only variables (i.e., not fields) for the event source are used when adding/removing event ties; otherwise, this will not result in a "weak" listener. The instance type should also be disposable and should detach the event listener in the finalizer or a memory leak of WeakEventListener objects may occur.

Constructors

WeakEventListener(TInstance, EventAction, Action<WeakEventListener<TInstance, TEventArgs>>)

Initializes an instance of the class.

public WeakEventListener(TInstance instance, WeakEventListener<TInstance, TEventArgs>.EventAction onEventAction, Action<WeakEventListener<TInstance, TEventArgs>> onDetachAction)
Parameter Type Description
instance TInstance

The instance that owns the listener.

onEventAction WeakEventListener<TInstance, TEventArgs>.EventAction

The delegate to execute when the event is raised.

onDetachAction Action<WeakEventListener<TInstance, TEventArgs>>

The delegate to execute when the event is detached.

Properties

OnDetachAction

The delegate to execute when the event is detached.

public Action<WeakEventListener<TInstance, TEventArgs>>? OnDetachAction { get; }

Property Value

Action<WeakEventListener<TInstance, TEventArgs>>

OnEventAction

The delegate to execute when the event is raised.

public WeakEventListener<TInstance, TEventArgs>.EventAction? OnEventAction { get; }

Property Value

WeakEventListener<TInstance, TEventArgs>.EventAction

Methods

Detach()

Detaches the event listener from the event.

public void Detach()

OnEvent(object?, TEventArgs)

The method that handles the event.

public void OnEvent(object? source, TEventArgs eventArgs)
Parameter Type Description
source object

The source of the event.

eventArgs TEventArgs

The event arguments.

Inherited Members

Extension Methods