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

[XC] Compile bindings with Source #20610

Conversation

simonrozsival
Copy link
Member

@simonrozsival simonrozsival commented Feb 15, 2024

Description of Change

Currently, we're skipping compilation of bindings which have Source. The reason was that there wasn't support for relative binding sources until recently. Since #20415 has been merged into net9.0, we can now safely compile bindings which have a Source property.

Issues Fixed

Contributes to #20266

@simonrozsival simonrozsival requested a review from a team as a code owner February 15, 2024 14:09
@jsuarezruiz jsuarezruiz added the area-xaml XAML, CSS, Triggers, Behaviors label Feb 15, 2024
@StephaneDelcroix StephaneDelcroix self-assigned this Feb 16, 2024
@StephaneDelcroix
Copy link
Contributor

StephaneDelcroix commented Feb 16, 2024

we can not start compiling Bindings with Source before we support x:DataType at the binding level, or we'll get issues with syntax like this

<Label x:Name="other" />
<Label x:DataType="viewModel" 
    Text={Binding foo} //binding on view model.foo
    TextColor={Binding TextColor, Source={x:Refererence other}} // will fail
/>

Also, in case the source can be determined (like x:Reference), we might not even need a DataType to proceed with compilation

@simonrozsival
Copy link
Member Author

simonrozsival commented Feb 16, 2024

I'm not sure what exactly you mean. This binding is compiled correctly using the code in this branch:

<Label
    Text="{Binding Text, x:DataType=Button, Source={x:Reference CounterBtn}}"
    Style="{StaticResource SubHeadline}"
    SemanticProperties.HeadingLevel="Level2"
    SemanticProperties.Description="Welcome to dot net Multi platform App U I" />

<Button
    x:Name="CounterBtn"
    Text="Click me" 
    SemanticProperties.Hint="Counts the number of times you click"
    Clicked="OnCounterClicked"
    HorizontalOptions="Fill" />

Decompiled assembly:

// ...
referenceExtension.Name = "CounterBtn";
XamlServiceProvider xamlServiceProvider2 = new XamlServiceProvider();
Type typeFromHandle3 = typeof(IProvideValueTarget);
object[] array2 = new object[0 + 5];
array2[0] = bindingExtension;
array2[1] = label2;
array2[2] = verticalStackLayout;
array2[3] = scrollView;
array2[4] = mainPage;
object obj2;
xamlServiceProvider2.Add(typeFromHandle3, obj2 = new SimpleValueTargetProvider(array2, typeof(BindingExtension).GetRuntimeProperty("Source"), new NameScope[6] { nameScope, nameScope, nameScope, nameScope, nameScope, nameScope }, false));
xamlServiceProvider2.Add(typeof(IReferenceProvider), obj2);
Type typeFromHandle4 = typeof(IXamlTypeResolver);
XmlNamespaceResolver xmlNamespaceResolver2 = new XmlNamespaceResolver();
xmlNamespaceResolver2.Add("", "http://schemas.microsoft.com/dotnet/2021/maui");
xmlNamespaceResolver2.Add("x", "http://schemas.microsoft.com/winfx/2009/xaml");
xamlServiceProvider2.Add(typeFromHandle4, new XamlTypeResolver(xmlNamespaceResolver2, typeof(MainPage).Assembly));
xamlServiceProvider2.Add(typeof(IXmlLineInfoProvider), new XmlLineInfoProvider(new XmlLineInfo(22, 17)));
object source = ((IMarkupExtension)referenceExtension).ProvideValue((IServiceProvider)xamlServiceProvider2);
bindingExtension.Source = source;
bindingExtension.Path = "Text";
bindingExtension.TypedBinding = new TypedBinding<Button, string>((Button P_0) => (P_0 != null) ? (P_0.Text, true) : default((string, bool)), delegate(Button P_0, string P_1)
{
    if (P_0 != null)
    {
        P_0.Text = P_1;
    }
}, new Tuple<Func<Button, object>, string>[1]
{
    new Tuple<Func<Button, object>, string>((Button P_0) => P_0, "Text")
});
BindingBase binding = ((IMarkupExtension<BindingBase>)bindingExtension).ProvideValue((IServiceProvider)null);
label2.SetBinding(Label.TextProperty, binding);
// ...

Also, in case the source can be determined (like x:Reference), we might not even need a DataType to proceed with compilation

I agree that that should be supported as well. But I think that could be implemented in a separate PR.

@simonrozsival
Copy link
Member Author

BTW looking at the decompiled code more closely, it doesn't seem we don't compile the x:Reference markup extensions at all so we aren't able to determine the type of the source at the moment. At the same time, in this case it should be trivial to compile the reference, so I'm a bit surprised we don't do that.

Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build is failing for windows. Also if you can rebase :)

Issue9417.xaml(43,37): XamlC error XFC0045: Binding: Property "BindingContext" not found on "Microsoft.Maui.Controls.ControlGallery.Issues.Issue9417Model". [D:\a\_work\1\s\src\Compatibility\ControlGallery\src\Core\Compatibility.ControlGallery.Core.csproj]
Issue11764.xaml(29,37): XamlC error XFC0045: Binding: Property "BindingContext" not found on "System.String". [D:\a\_work\1\s\src\Compatibility\ControlGallery\src\Core\Compatibility.ControlGallery.Core.csproj]
Issue11831.xaml(57,41): XamlC error XFC0045: Binding: Property "BindingContext" not found on "System.String". [D:\a\_work\1\s\src\Compatibility\ControlGallery\src\Core\Compatibility.ControlGallery.Core.csproj]
    7010 Warning(s)
    3 Error(s)

@simonrozsival
Copy link
Member Author

@rmarinho the remaining failing tests seem to be unrelated

@rmarinho rmarinho merged commit 160a484 into dotnet:net9.0 Mar 11, 2024
47 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants