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.28k stars 1.76k forks source link

[XC] Infer x:DataType from BindingContext or BindingExtension.Source #21834

Open simonrozsival opened 7 months ago

simonrozsival commented 7 months ago

In certain scenarios, XamlC should be able to infer the x:DataType based on the value passed to BindingContext on any XAML element or to Source on BindingExtension elements:

Explicitly declared x:DataType should always take precedence over the inferred type. In the case when there is a mismatch, we should consider emitting a warning.

Are there any other cases which we could infer?

/cc @StephaneDelcroix

hansmbakker commented 2 months ago

Will this still be part of .NET 9? I'm afraid a lot of people will run into this via #23711 if they are upgrading from .NET 9

simonrozsival commented 2 months ago

@hansmbakker it's not likely this will be in .NET 9. It's true that the issue you linked shows that people might find it difficult to upgrade to .NET 9 in certain scenarios and we need to do something about it (most likely better warning messages).

I don't think implementing the type inference the way I outlined it in this issue would fix that issue though. The problem there is that the Path can only be resolved at runtime via reflection and it cannot be compiled ahead of time because we don't know the type of object BindingContext and we don't have a way to specify a cast at the moment.

awasilik commented 2 months ago

Without this we'll not be able to compile app with .net9 as we need to use binding to Source{x:Reference

MartyIX commented 2 months ago

@awasilik This should work:

-<RowDefinition Height="{Binding HeightRequest, Source={x:Reference thisControl}}"/>
+<RowDefinition Height="{Binding HeightRequest, Source={x:Reference thisControl}, x:DataType={x:Null}}"/>

However, yes, it requires manual changes in code.