dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.26k stars 1.76k forks source link

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

Open egvijayanand opened 1 day ago

egvijayanand commented 1 day ago

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.

jkurdek commented 22 hours ago

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: https://github.com/dotnet/maui/issues/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 commented 21 hours ago

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.