Source generation for BindableProperty
when making custom ContentView
#458
Replies: 9 comments 13 replies
-
Transferring this to the MAUI toolkit, since |
Beta Was this translation helpful? Give feedback.
-
Thanks @Sergio0694 . @KafkaWannaFly does this API looks good fir you? |
Beta Was this translation helpful? Give feedback.
-
Some ideas on how to implement it and don't. At first, it was suggested to create a field and use it to "store" the attribute and generate the BP. Like this snippet // User code this
[BindableProperty]
string cardTitle; TBH I don't like this approach at first glance because the compiler-generated code will not remove those fields, since they are used by the Attributes, can these fields have any perf impact? Like increasing the size of the Instead, I would suggest using something that will be used (method, ctor and/or class) so I thought of a couple of ways of doing it: 1. Using attributes at the class levelThe class will be there and will be used, so we can take advantage of that: [BPSourceGen.BPCreation(PropertyName = "CardTitle", ReturnType = typeof(string), OwnerType = typeof(CardView), DefaultValue = string.Empty, PropertyChangedMethodName = "Invalidate")]
[BPSourceGen.BPCreation(PropertyName = "CardDescription", ReturnType = typeof(string)]
public partial class CardView : ContentView
{
public static void Invalidate() { } // OnPropertyChanged method
} The 2. Using attributes at the ctor levelpublic partial class CardView : ContentView
{
[BPSourceGen.BPCreation(PropertyName = "CardTitle", ReturnType = typeof(string), OwnerType = typeof(CardView), DefaultValue = string.Empty, PropertyChangedMethodName = "Invalidate")]
[BPSourceGen.BPCreation(PropertyName = "CardDescription", ReturnType = typeof(string)]
public CardView() { }
public static void Invalidate() { } // OnPropertyChanged method
} The same as the 3. Using attributes at PropertyChanged methodThis is an addition to the last suggestions, so let's say that the user will implement the public partial class CardView : ContentView
{
[BPSourceGen.BPCreation(PropertyName = "CardDescription", ReturnType = typeof(string)]
public CardView() { }
[BPSourceGen.BPCreation(PropertyName = "CardTitle", ReturnType = typeof(string), OwnerType = typeof(CardView), DefaultValue = string.Empty)]
public static void Invalidate() { } // OnPropertyChanged method
} |
Beta Was this translation helpful? Give feedback.
-
I use this lib: https://github.com/rrmanzano/maui-bindableproperty-generator |
Beta Was this translation helpful? Give feedback.
-
I also come to this implementation for the idea: https://github.com/KafkaWannaFly/BindableProps |
Beta Was this translation helpful? Give feedback.
-
I'm writing a library that perfectly fits in this thread's title, but aims to do something slightly different. I give an example to explain that. Let's say that inside your I created an attribute that, applied to the custom control class and given some required parameters, creates a bindable property linked to the Link to the repo: https://github.com/Riccardo11/CCPropGen. This is a code example. using Maui.CCPropGen;
namespace CCPropGen.Maui.Demo;
// Attribute parameters:
// - Control name
// - Control type
// - Property name
// - Property type
[CCPropGen("EntryControl", typeof(Entry), "Text", typeof(string))]
public partial class CCPropGenEntry : ContentView
{
public CCPropGenEntry()
{
InitializeComponent();
// Currently needed to set binding contexts correctly
InitializeCCPropGen();
}
} It's far from being ready to be used in a real environment, but I would appreciate your thoughts about it. |
Beta Was this translation helpful? Give feedback.
-
Closed as per the Community Stand-up discussion here: https://youtu.be/N9wMcBP4jtg?t=2889 |
Beta Was this translation helpful? Give feedback.
-
This was talked about on the Community Meet-up chat for July 2024 and it agreed this would be re-opened and we will look at the PR from @pictos and see if it solves the need. |
Beta Was this translation helpful? Give feedback.
-
As PR #1321 has been implemented internally for the maintainers to dogfood and it has been discussed on the September monthly stand-up I am closing this discussion at this time. |
Beta Was this translation helpful? Give feedback.
-
Overview
I try to make a custom UI control for my MAUI app and see that I have to make so many boiler-plate codes. I take an example from https://docs.microsoft.com/en-us/dotnet/maui/user-interface/controls/contentview.
You see, for each property, we have to declare a
BindableProperty
. I think we could auto-generate code like what we're having withBindableObject
,ICommand
, etc.I have browsed around but didn't see anything like this so you guys could create one.
API breakdown
Usage example
From the example above but we will re-write like this
And the source code is generated would look like this
Breaking change?
No
Alternatives
I try but can't find any
Additional context
The need for this feature occurs when we want to create a custom UI component and expose some of its fields as mentioned above
Help us help you
No, just wanted to propose this
Beta Was this translation helpful? Give feedback.
All reactions