maui-bindableproperty-generator icon indicating copy to clipboard operation
maui-bindableproperty-generator copied to clipboard

Think about making the AutoBindable an Attribute for the class, so you do not need a placeholder field

Open juwens opened this issue 2 years ago • 3 comments

This :

    [AutoBindable("Header", typeof(string))] // c#10
    [AutoBindable<string>("Header")] // with C#11 generic attributes feature
    public partial class CustomEntry : ContentView
    {
    }

Instead of this:

    public partial class CustomEntry : ContentView
    {
        [AutoBindable]
        private readonly string _placeholder;
    }

Advantage:

  • no unnecessary field is needed; in this sense it is more clean

Downside:

  • it is a breaking change, which forces devs to adjust their code

juwens avatar Feb 02 '23 14:02 juwens

I think it makes a lot of sense. While I like having properties defined inside the class block, having to declare an unused field requires turning off some compiler warnings.

It should be possible to implement this without breaking the existing attribute and either offer both options or provide a transition period for developers to adapt

paulvarache avatar Apr 20 '23 09:04 paulvarache

How do you think if the generator will base on the handler methods?

e.g.

// ControlX.properties.cs
partial class ControlX {
    [AutoBindable<bool>]
     private static void HandlePropertyXChanged(BindableObject obj, object oldValue, object newValue) {}

     private static void HandlePropertyXChanging(BindableObject obj, object oldValue, object newValue) {}
     
    [AutoBindable<string>()]
     private static void HandlePropertyYChanging(BindableObject obj, object oldValue, object newValue) {}
}

The generator will generate

// ControlX.g.cs
partial class ControlX {
    public static BindableProperty PropertyXProperty = BindableProperty.Create(
        nameof(PropertyX),
        typeof(ControlX),
        typeof(bool),
        true,
        propertyChanged: HandlePropertyXChanged,
        propertyChanging: HandlePropertyXChanging
    );
    public bool PropertyX {
        get => (bool)GetValue(PropertyXProperty);
        get => SetValue(PropertyXProperty, value);
    }
    
    public static BindableProperty PropertyYProperty = BindableProperty.Create(
        nameof(PropertyY),
        typeof(ControlX),
        typeof(string),
        default,
        propertyChanging: HandlePropertyYChanging
    );
    public string PropertyY {
        get => (string)GetValue(PropertyYProperty);
        get => SetValue(PropertyYProperty, value);
    }
}

tuyen-vuduc avatar Jul 19 '23 02:07 tuyen-vuduc

And we should remove MAUI dependency because with .NET6/C# 10, we can use global usings for namespaces in generated code

tuyen-vuduc avatar Jul 19 '23 03:07 tuyen-vuduc