-
Notifications
You must be signed in to change notification settings - Fork 356
Description
Overview
A class for weakly subscribing to events is a useful concept that has been implemented many times in various projects.
I currently use Xamarin Forms with Xamarin Community Toolkit, and rely on the ObservableObject
implementation that uses WeakEventManager
for INotifyPropertyChanged.PropertyChanged
.
Maui Community Toolkit however does not have its own ObservableObject
, instead directing people to use the one here (an idea I fully support). However the one here does not support weak subscribing (because there is no WeakEventManager
).
I propose adding WeakEventManager
- probably the one one currently implemented in Maui (https://github.com/dotnet/maui/blob/aa2d11137e8b3226d85a39da4f605bf26ab42aa0/src/Core/src/WeakEventManager.cs), and then adding a way for ObservableObejct
to (optionally) use it.
(I have a separate issue for that: #80)
API breakdown
(Copied from Maui)
public class WeakEventManager
{
public void AddEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [CallerMemberName] string eventName = "")
where TEventArgs : EventArgs
{
}
public void AddEventHandler(Delegate? handler, [CallerMemberName] string eventName = "")
{
}
public void HandleEvent(object sender, object args, string eventName)
{
}
public void RemoveEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [CallerMemberName] string eventName = "")
where TEventArgs : EventArgs
{
}
public void RemoveEventHandler(Delegate? handler, [CallerMemberName] string eventName = "")
{
}
}
Usage example
public event PropertyChangedEventHandler? PropertyChanged
{
add => _weakEventManager.AddEventHandler(value);
remove => _weakEventManager.RemoveEventHandler(value);
}
Breaking change?
No
Alternatives
There is a proposal to just add this to the BCL:
dotnet/runtime#61517
Additional context
No response
Help us help you
No, just wanted to propose this.
If using the Maui one unchanged is desirable, I'd be able to make such a PR.