Click or drag to resize
DigitalRuneWeakEventHandlerTEventHandler, TEventArgsRegisterTSender, TListener Method
Registers an event handler that works with a weak reference to the target object.

Namespace: DigitalRune
Assembly: DigitalRune (in DigitalRune.dll) Version: 1.20.0.0 (1.20.1.14552)
Syntax
public static IDisposable Register<TSender, TListener>(
	TSender sender,
	TListener listener,
	Func<EventHandler<TEventArgs>, TEventHandler> conversion,
	Action<TSender, TEventHandler> addHandler,
	Action<TSender, TEventHandler> removeHandler,
	Action<TListener, Object, TEventArgs> forwardEvent
)
where TSender : class
where TListener : class

Parameters

sender
Type: TSender
The object that provides the event.
listener
Type: TListener
The object that listens to the event.
conversion
Type: SystemFuncEventHandlerTEventArgs, TEventHandler
A function used to convert the given event handler to a delegate compatible with the underlying .NET event.
addHandler
Type: SystemActionTSender, TEventHandler
A callback method that adds an event handler to the event.
removeHandler
Type: SystemActionTSender, TEventHandler
A callback method that removes an event handler from the event.
forwardEvent
Type: SystemActionTListener, Object, TEventArgs
A callback method that forwards the event to the actual event handler.

Type Parameters

TSender
The type of the sender.
TListener
The type of the listener.

Return Value

Type: IDisposable
An IDisposable which can be used to detach the weak event handler from the event.
Exceptions
ExceptionCondition
ArgumentNullExceptionsender, addHandler, removeHandler, listener, or forwardEvent is .
Remarks
Access to the event and to the real event handler is done through lambda expressions. The code holds strong references to these expressions, so they must not capture any variables of the target object (listener)!
Examples
C#
WeakEventHandler<PropertyChangedEventHandler, PropertyChangedEventArgs>.Register(
    textDocument,
    this,
    handler => new PropertyChangedEventHandler(handler),
    (sender, pcHandler) => sender.PropertyChanged += pcHandler,
    (sender, pcHandler) => sender.PropertyChanged -= pcHandler,
    (listener, sender, eventArgs) => listener.OnDocumentChanged(sender, eventArgs));
See Also