-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[XSG][BindingSourceGen] Add support for CommunityToolkit.Mvvm ObservablePropertyAttribute #33028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PureWeen
merged 15 commits into
inflight/current
from
copilot/add-observable-property-support
Jan 14, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
40a385d
Initial plan
Copilot 01d3893
Initial plan for ObservablePropertyAttribute support
Copilot babe40c
Add ObservableProperty support to BindingSourceGen and SourceGen
Copilot 61f2123
Apply code formatting
Copilot 059b600
Remove unrelated file and finalize changes
Copilot ff72b9e
Add XAML unit tests for ObservableProperty binding support
Copilot 30124bc
Revert "Add XAML unit tests for ObservableProperty binding support"
simonrozsival 404d273
Remove HWV.js.map
simonrozsival 741b99e
Address code review feedback for ObservableProperty support
Copilot 63e48ae
Remove unrelated HybridWebView.js.map file
Copilot bbce3f3
Fix test assertions to check handlers instead of method signature
Copilot 8164930
Remove unrelated HybridWebView.js.map file
Copilot f23e92e
Migrate tests to XUnit
simonrozsival 7557872
Revert unrelated test file changes
simonrozsival cb00d50
Remove uninteresting tests
simonrozsival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
src/Controls/tests/BindingSourceGen.UnitTests/ObservablePropertyTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.Maui.Controls.BindingSourceGen; | ||
| using Xunit; | ||
|
|
||
| namespace BindingSourceGen.UnitTests; | ||
|
|
||
| public class ObservablePropertyTests | ||
| { | ||
| // Note: Expression-based bindings work with ObservableProperty through type inference in | ||
| // GetLambdaReturnType. The generated interceptor code will be correct. In tests, | ||
| // we'll see compilation errors because the actual generated property doesn't exist | ||
| // (CommunityToolkit.Mvvm's source generator isn't running in the test environment), | ||
| // but the interceptor code itself is generated correctly. | ||
|
|
||
| [Fact] | ||
| public void GenerateBindingToObservablePropertyFromCamelCaseField() | ||
| { | ||
| var source = """ | ||
| using Microsoft.Maui.Controls; | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace CommunityToolkit.Mvvm.ComponentModel | ||
| { | ||
| [System.AttributeUsage(System.AttributeTargets.Field)] | ||
| public class ObservablePropertyAttribute : System.Attribute { } | ||
| } | ||
|
|
||
| namespace TestApp | ||
| { | ||
| public class MyViewModel | ||
| { | ||
| [CommunityToolkit.Mvvm.ComponentModel.ObservableProperty] | ||
| private string? name; | ||
| // Name property will be generated by CommunityToolkit.Mvvm | ||
| } | ||
|
|
||
| public class TestCode | ||
| { | ||
| public void Test() | ||
| { | ||
| var label = new Label(); | ||
| label.SetBinding(Label.TextProperty, static (MyViewModel vm) => vm.Name); | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| var result = SourceGenHelpers.Run(source); | ||
|
|
||
| // The binding should be generated successfully with ObservableProperty inference | ||
| Assert.NotNull(result.Binding); | ||
|
|
||
| // Verify the generated interceptor code contains the correct getter and setter references | ||
| var allGeneratedCode = string.Join("\n\n", result.GeneratedFiles.Values); | ||
| // Check that the handler contains the property access | ||
| Assert.Contains("new(static source => source, \"Name\")", allGeneratedCode, System.StringComparison.Ordinal); | ||
| // Check that setter assigns to .Name | ||
| Assert.Contains("source.Name = value;", allGeneratedCode, System.StringComparison.Ordinal); | ||
|
|
||
| // Note: There will be compilation errors because Name doesn't actually exist, | ||
| // but the interceptor code itself is generated correctly. In real usage with | ||
| // CommunityToolkit.Mvvm, the property would exist and compile successfully. | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GenerateBindingToObservablePropertyFromUnderscorePrefixedField() | ||
| { | ||
| var source = """ | ||
| using Microsoft.Maui.Controls; | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace CommunityToolkit.Mvvm.ComponentModel | ||
| { | ||
| [System.AttributeUsage(System.AttributeTargets.Field)] | ||
| public class ObservablePropertyAttribute : System.Attribute { } | ||
| } | ||
|
|
||
| namespace TestApp | ||
| { | ||
| public class MyViewModel | ||
| { | ||
| [CommunityToolkit.Mvvm.ComponentModel.ObservableProperty] | ||
| private string? _title; | ||
| // Title property will be generated by CommunityToolkit.Mvvm | ||
| } | ||
|
|
||
| public class TestCode | ||
| { | ||
| public void Test() | ||
| { | ||
| var label = new Label(); | ||
| label.SetBinding(Label.TextProperty, static (MyViewModel vm) => vm.Title); | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| var result = SourceGenHelpers.Run(source); | ||
|
|
||
| // The binding should be generated successfully with ObservableProperty inference | ||
| Assert.NotNull(result.Binding); | ||
|
|
||
| // Verify the generated interceptor code contains the correct getter and setter references | ||
| var allGeneratedCode = string.Join("\n\n", result.GeneratedFiles.Values); | ||
| // Check that the handler contains the property access | ||
| Assert.Contains("new(static source => source, \"Title\")", allGeneratedCode, System.StringComparison.Ordinal); | ||
| // Check that setter assigns to .Title | ||
| Assert.Contains("source.Title = value;", allGeneratedCode, System.StringComparison.Ordinal); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GenerateBindingToObservablePropertyCollection() | ||
| { | ||
| var source = """ | ||
| using Microsoft.Maui.Controls; | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace CommunityToolkit.Mvvm.ComponentModel | ||
| { | ||
| [System.AttributeUsage(System.AttributeTargets.Field)] | ||
| public class ObservablePropertyAttribute : System.Attribute { } | ||
| } | ||
|
|
||
| namespace TestApp | ||
| { | ||
| public class Tag { } | ||
|
|
||
| public class MyViewModel | ||
| { | ||
| [CommunityToolkit.Mvvm.ComponentModel.ObservableProperty] | ||
| private ObservableCollection<Tag> _tags = new(); | ||
| // Tags property will be generated by CommunityToolkit.Mvvm | ||
| } | ||
|
|
||
| public class TestCode | ||
| { | ||
| public void Test() | ||
| { | ||
| var label = new Label(); | ||
| label.SetBinding(Label.BindingContextProperty, static (MyViewModel vm) => vm.Tags); | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| var result = SourceGenHelpers.Run(source); | ||
|
|
||
| // The binding should be generated successfully with ObservableProperty inference | ||
| Assert.NotNull(result.Binding); | ||
|
|
||
| // Verify the generated interceptor code contains the correct getter and setter references | ||
| var allGeneratedCode = string.Join("\n\n", result.GeneratedFiles.Values); | ||
| // Check that the handler contains the property access | ||
| Assert.Contains("new(static source => source, \"Tags\")", allGeneratedCode, System.StringComparison.Ordinal); | ||
| // Check that setter assigns to .Tags | ||
| Assert.Contains("source.Tags = value;", allGeneratedCode, System.StringComparison.Ordinal); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.