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

[XamlC] Produce warning when Binding inside of DataTemplate inherits x:DataType from outside of the template #22714

Closed
simonrozsival opened this issue May 29, 2024 · 4 comments
Assignees
Labels
area-xaml XAML, CSS, Triggers, Behaviors t/enhancement ☀️ New feature or request
Milestone

Comments

@simonrozsival
Copy link
Member

simonrozsival commented May 29, 2024

I think XamlC should produce a warning when there is a {Binding ...} inside of a DataTemplate which inherits x:DataType from outside of the template. Consider the following example:

<ScrollView x:DataType="local:PageViewModel">
    <StackLayout BindableLayout.ItemsSource="{Binding Items}">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <Label Text="{Binding .}" />
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
</ScrollView>

My suggestion is to report a warning something like

XC0024: Binding might be compiled incorrectly because it is using an x:DataType local:PageViewModel that is defined outside of the current DataTemplate scope. Consider adding x:DataType directly to the DataTemplate. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information.

... and add a paragraph describing this issue to the linked documentation page.

Related to #22056

/cc @StephaneDelcroix @ivanpovazan @PureWeen @mattleibow

@simonrozsival simonrozsival added the area-xaml XAML, CSS, Triggers, Behaviors label May 29, 2024
Copy link
Contributor

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@ivanpovazan
Copy link
Member

For reference:
@PureWeen recently fixed an issue with the same root cause in: rachelkang/recipeSearch@b63c3a7

@ivanpovazan
Copy link
Member

FWIW:
Another idea for the warning message could be that our recommendation is to always specify x:DataType for bindings in templates explicitly, as if it is omitted it could inherit x:DataType from a parent scope and cause runtime failures.

@simonrozsival
Copy link
Member Author

The implementation was merged into the net9.0 branch

@github-project-automation github-project-automation bot moved this from Todo to Done in MAUI SDK Ongoing Jul 1, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 31, 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 t/enhancement ☀️ New feature or request
Projects
Status: Done
Development

No branches or pull requests

4 participants