Xamarin Community Toolkit WeakEventManager
An event implementation that enables the garbage collector to collect an object without needing to unsubscribe event handlers.
Inspired by Xamarin.Forms.WeakEventManager.
Syntax
public WeakEventManager<TEventArgs>()
Methods
Methods | Return Type | Description |
---|---|---|
AddEventHandler(EventHandler<TEventArgs>, string eventName) | void | Adds the event handler. |
AddEventHandler(Action<TEventArgs>, string eventName) | void | Adds the event handler. |
RemoveEventHandler(EventHandler<TEventArgs>, string eventName) | void | Removes the event handler. |
RemoveEventHandler(Action<TEventArgs>, string eventName) | void | Removes the event handler. |
HandleEvent(object, TEventArgs, string | void | Invokes the event EventHandler. |
HandleEvent(TEventArgs, string | void | Invokes the event Action. |
RaiseEvent(object, TEventArgs, string | void | Invokes the event EventHandler. |
RaiseEvent(TEventArgs, string | void | Invokes the event Action. |
Examples
This section shows how to use this type.
Use EventHandler<T>
readonly WeakEventManager<string> _errorOcurredEventManager = new WeakEventManager<string>();
public event EventHandler<string> ErrorOcurred
{
add => _errorOcurredEventManager.AddEventHandler(value);
remove => _errorOcurredEventManager.RemoveEventHandler(value);
}
void OnErrorOcurred(string message) => _errorOcurredEventManager.RaiseEvent(this, message, nameof(ErrorOcurred));
Use Action<T>
readonly WeakEventManager<string> _weakActionEventManager = new WeakEventManager<string>();
public event Action<string> ActionEvent
{
add => _weakActionEventManager.AddEventHandler(value);
remove => _weakActionEventManager.RemoveEventHandler(value);
}
void OnActionEvent(string message) => _weakActionEventManager.RaiseEvent(message, nameof(ActionEvent));