maui-bindableproperty-generator
maui-bindableproperty-generator copied to clipboard
Think about making the AutoBindable an Attribute for the class, so you do not need a placeholder field
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
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
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);
}
}
And we should remove MAUI dependency because with .NET6/C# 10, we can use global usings
for namespaces in generated code