Skip to content
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

Why don't .NET MAUI 9 C# compiled bindings support source-generated properties? #25988

Open
egvijayanand opened this issue Nov 20, 2024 · 4 comments
Labels
area-xaml XAML, CSS, Triggers, Behaviors proposal/open

Comments

@egvijayanand
Copy link
Contributor

Description

Why don't .NET MAUI 9 compiled bindings support source-generated properties?

When I tried to use the MVVM Toolkit feature for a source-generated property from a backing field and bound it to the newly defined SetBinding call, I received this build error message.

If XAML can work with this property binding, why is C# raising an issue? Support for SG-properties is very crucial in the current context.

Error message: The lambda result type cannot be resolved. Make sure that soruce generated fields / properties are not used in the path.

// Defined within the Page constructor
this.SetBinding(Page.TitleProperty, static (MainViewModel vm) => vm.Title);

Public API Changes

Ability to resolve the compilation context with the SG-generated source files.

Intended Use-Case

Potential use of source-generated (SG) toolkits, including the officially supported CommunityToolkit.Mvvm package.

@egvijayanand egvijayanand changed the title Why don't .NET MAUI 9 compiled bindings support source-generated properties? Why don't .NET MAUI 9 C# compiled bindings support source-generated properties? Nov 20, 2024
@jkurdek jkurdek added the area-xaml XAML, CSS, Triggers, Behaviors label Nov 20, 2024
@jkurdek
Copy link
Member

jkurdek commented Nov 20, 2024

Hi @egvijayanand,
we are happy to see you use the new compiled bindings and thank you for raising this issue!

The new compiled bindings are source generated. One design premise of source generators is that they work independently. That is source generator A cannot see the output of source generator B when generating code. So when the compiled bindings are generated we cannot resolve any type information about source generated fields.

The XAML style string bindings are resolved using reflection at runtime, so they don't experience those kind of problems.

We are aware of this limitation as it also occurs when using XamlG generated code:
#23531

As for workaround you could either revert to string based bindings (this might affect trimming) or writing the appropriate TypedBinding manually. Under the hood, the new expression-based SetBinding creates an instance of TypedBinding which is then used to call SetBinding(property, typedBinding).

@egvijayanand
Copy link
Contributor Author

Why doesn't .NET MAUI assume the existence of such a property, proceed with source generation, and let the build process throw an error if it doesn't match the expectations? This would prevent a cyclical issue.

@jkurdek
Copy link
Member

jkurdek commented Nov 21, 2024

In order to assume the existence of some source generated property MAUI would basically need to redo the work of the relevant source generator. So in other words merge them with the MAUI bindings source generator. While yes, this would solve the problem, there is a number of things that would be problematic about it. First, for which source generators do we make assumptions about. How do we choose this set. Second, this would introduce an extremely tight coupling between the generators, every change to them would have to be reflected in MAUI bindings source generator.

@egvijayanand
Copy link
Contributor Author

This is clearly a deadlock situation. A potential solution could be to delay the MAUI source generator until build time, allowing other independent source generators, like MVVM Toolkit, to complete their processing. This way, type information is fully available for the MAUI source generator, clearing the way for the project build. Otherwise, it results in an indefinite wait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-xaml XAML, CSS, Triggers, Behaviors proposal/open
Projects
None yet
Development

No branches or pull requests

2 participants