Xamarin.forms: Make `Xamarin.Forms.WeakEventManager` Public

Created on 15 Dec 2018  Â·  11Comments  Â·  Source: xamarin/Xamarin.Forms

Summary

Xamarin.Forms.WeakEventManager is an excellent example of how to leverage the Weak Event pattern to avoid memory leaks.

I'd love to leverage it in my Xamarin.Forms apps, but the class is currently internal.

API Changes

Current API

namespace Xamarin.Forms
{
    internal class WeakEventManager`

Requested API

namespace Xamarin.Forms
{
    public class WeakEventManager

Intended Use Case

This will allow Xamarin.Forms devs to to implement the Weak Event pattern without needing to implement a custom version or rely on 3rd party dependancy.

invalid enhancement âž•

All 11 comments

I also recommend tweaking the API for AddEventHandler and RemoveEventHandler to use [CallerMemberName]

Current API

public void AddEventHandler(string eventName, EventHandler handler);
public void AddEventHandler<TEventArgs>(string eventName , EventHandler<TEventArgs> handler);
public void RemoveEventHandler(string eventName, EventHandler handler);
public void RemoveEventHandler<TEventArgs>(string eventName, EventHandler<TEventArgs> handler);

Current Usage

public event EventHandler CanExecuteChanged
{
    add => _weakEventManager.AddEventHandler(nameof(CanExecuteChanged), value);
    remove => _weakEventManager.RemoveEventHandler(nameof(CanExecuteChanged), value);
}

Recommended API

public void AddEventHandler(EventHandler handler, [CallerMemberName] string eventName = "");
public void AddEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [CallerMemberName] string eventName = "");
public void RemoveEventHandler(EventHandler handler, [CallerMemberName] string eventName = "");
public void RemoveEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [CallerMemberName] string eventName = "");

Recommended Usage

public event EventHandler CanExecuteChanged
{
    add => _weakEventManager.AddEventHandler(value);
    remove => _weakEventManager.RemoveEventHandler(value);
}

Strange to have this in the xamarin.forms namespace though isn't it? Looking at the api, it doesn't look good enough to me to add to .net standard, with all the string identifiers. The current situation, where you need to copy and paste it or use a lib, may be best.

it's indeed a very useful pattern, but it's not the mission of XF to deliver, and support this.

@brminnick thanks for the API change suggestion. good one

@StephaneDelcroix
No worries - that's understandable

You can also drop the constraint for AddEvent<TEventArgs> and RemoveEvent<TEventArgs>, because System.EventHandler<TEventArgs> doesn't constrain its TEventArgs.

Current API

public void RemoveEventHandler<TEventArgs>(string eventName, EventHandler<TEventArgs> handler) where TEventArgs : EventArgs;

public void AddEventHandler<TEventArgs>(string eventName, EventHandler<TEventArgs> handler) where TEventArgs : EventArgs;

Recommended API

public void RemoveEventHandler<TEventArgs>(string eventName, EventHandler<TEventArgs> handler);

public void AddEventHandler<TEventArgs>(string eventName, EventHandler<TEventArgs> handler);

@brminnick nah, let's keep the constraint

@StephaneDelcroix then to who the mission is? in order to open a issue there and track the support of this must have feature?

@danielmeza I ran with the Xamarin.Forms implementation and added it to this .NET Standard NuGet Package:
https://github.com/brminnick/AsyncAwaitBestPractices#weakeventmanager

I added type safety, WeakEventManager<T>, and added support for more types of events: event Delegate, event EventHandler, event EventHander<T>, event Action and event Action<T>

@brminnick thanks for you package, but this should be provide by the framework or at least in some official library like in WPF or Windows Forms, thus I'm asking to who is the mission. I implement it by cloning the class in my own package but it should be done public some way.

@danielmeza Your suggestion that this should be in a specific UI library doesn't make sense. Obviously if they implemented it in WPF you would not be able to use it in Xamarin.Forms projects, .Net Standard projects, anything that is not a WPF project. Since the code involved does not have a UI dependency, it is not the responsibility of any UI library, and should be done as a .Net Standard project, as @brminnick has correctly done.

If you want to track official support for weak event handlers in .Net, https://github.com/dotnet/corefx/issues/11898 is a good start.

@charlesroddie https://github.com/dotnet/corefx/issues/11898 this is was I talking about, not to include this in any UI library, thanks for the link.

Was this page helpful?
0 / 5 - 0 ratings