BindableProperty [PropertyChanged] attribute enhancement #3001
Replies: 4 comments
-
|
Hey Stephen! I plan on creating an Analyzer that will generate a compiler error if a developer supplies a method name that is using an invalid method signature. This analyzer will ensure that developers always use the correct return type and parameter types, and it'll also ensure the method is The Analyzer should cover scenarios where a developer forgets to supply a |
Beta Was this translation helpful? Give feedback.
-
|
Hi Brandon - thanks for the rapid response! An Analyzer would definitely be useful for catching mistakes with the current static method setup. What I'm suggesting, though, is a shift away from requiring static methods altogether. In my proposed generator changes, a static wrapper would handle casting the bindable object to the right type, so developers can just write normal instance methods and let the generator take care of the rest. |
Beta Was this translation helpful? Give feedback.
-
|
|
Beta Was this translation helpful? Give feedback.
-
Redacted comment (it is copied/moved to root comment)public partial class TestClass : View
{
[BindableProperty(
DefaultValue = 42,
PropertyChangedMethodName = "OnPropertyChanged",
PropertyChangingMethodName = "OnPropertyChanging")]
public partial int Value { get; set; }
[PropertyChanged]
partial void OnPropertyChanged(int value) { }
partial void OnPropertyChanged(int oldValue, int newValue) { }
[PropertyChanging]
partial void OnPropertyChanging(int value) { }
partial void OnPropertyChanging(int oldValue, int newValue) { }
}The public partial class TestClass
{
static void OnPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
((TestCase)b).OnPropertyChanged((int)newValue);
((TestCase)b).OnPropertyChanged((int)oldValue, (int) newValue);
}
partial void OnPropertyChanged(int value);
partial void OnPropertyChanged(int oldValue, int newValue);
static void OnPropertyChanging(BindableObject bindable, object oldValue, object newValue)
{
((TestCase)b).OnPropertyChanging((int)newValue);
((TestCase)b).OnPropertyChanging((int)oldValue, (int) newValue);
}
partial void OnPropertyChanging(int value);
partial void OnPropertyChanging(int oldValue, int newValue);
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Revision History: Original 14 Dec 2025, Rewrite 26 Jan 2026, Rewrite 2 Feb 2026
Introduce new optional attributes:
[PropertyChanged],[PropertyChanging],[ValidateValue],[CoerceValue], and[CreateDefaultValue].The new attributes introduce five optional capabilities. Two of them enable non-static partial methods, and the other three enable non-static methods. These attributes simply give developers the choice to opt into non-static patterns, while still allowing them to use the traditional static methods exactly as before. For convenience, the example below shows what opting in might look like.
If
[PropertyChanged]/[PropertyChanging]are attached to partial methods, then the source generator will generate the static method and invoke those partial methods:If
[PropertyChanged]/[PropertyChanging]are attached to regular methods, then the source generator will create the static to non-static wrappers:For
[ValidateValue],[CoerceValue],[CreateDefaultValue]the source generator will create static to non-static wrappers:Redacted original proposal
Instead of asking developers to write static methods for source generators, we could make things easier by letting them use regular instance methods. The benefit is that the methods are simpler, requiring no BindableProperty parameter or manual casting to the class type, since the source generator handles this automatically.For example:
Would generate a wrapper that includes the BindableObject bindable property:
Alternatives
There’s merit in revisiting whether some of these behaviors should be default for BindableProperty. Given how often
PropertyChangedandPropertyChangingare used, we might want to always generate the partial property wrappers.**Al
Tentative PR
Beta Was this translation helpful? Give feedback.
All reactions