-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 XC0045 on Reference Bindings pointed to XAML #23711
Comments
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:
|
@daltzctr thanks for the additional context. The problem in this case is that To overcome this issue, you need to use <Button Text="{Binding Name}" Command="{Binding Source={RelativeSource Mode=FindAncestorBindingContext, AncestorType={x:Type vm:MyViewModel}}, Path=MyCommand, x:DataType=vm:MyViewModel}" /> You can find more information in the docs. Does this solve your problem? |
Quote: "all bindings need to be compiled now" |
I have applied this workaround in my XAML which causes following Exception on Windows
|
As @nor0x mentioned, this has been a HUGE issue for the MCT TouchBehaviour. We are telling people to use |
I hope this ticket gets the attention it needs? Seems kinda serious, if this gets unfixed into the final .NET 9, right? |
@daltzctr That's not accurate. XamlC will try to compile the binding IF there is an
@Axemasta Relative source work just fine. You can see several examples in MAUI's unit tests (https://github.com/dotnet/maui/blob/main/src/Controls/tests/Core.UnitTests/RelativeSourceBindingTests.cs) and I also validated the binding in my previous comment and it also works correctly. If you find any issues with relative sources, please report it so we can fix it. I agree that using
@formerlymisterhenson The motivation to add support for compilation of bindings with explicitly set sources is to support trimming (see #18658 and #20610). When bindings aren't compiled, the trimmer doesn't see which properties will be needed at runtime and it can trim them. As a side benefit, compiled bindings improve performance. @StephaneDelcroix @Redth @jonathanpeppers @davidortinau I have some suggestions how to reduce the friction to the new behavior:
|
@simonrozsival would this issue be a good place to put a replication of the issue we are seeing in the MCT touch effect. Basically we cannot use relative source as it crashes with a similar stack trace to the original issue. I understand it's not a Maui control but we haven't been able to understand why it's breaking as it should work (the same did in xamarin). I can post a basic reproduction and happy to answer questions and help fix any issues, although they're possibly on our end! |
@Axemasta could you open a new issue? I think it would be better to keep those separate. |
Can a Reference binding really be to any object, or will it always be to an Element? I have never seen it tied to something that is not an Element. If the compiler always assumes that it is an Element, then this would be enough to provide access to the BindingContext, irrespective of whether casting is ever added as a feature. |
The problem is not finding
|
I see. The Here is one possible syntax:
It would know that the BindingContext is |
Today I tested the .NET 9p7 and I got an error on <HorizontalStackLayout IsVisible="{Binding Source={x:Reference toggleAll}, Path=IsVisible}">
<Button x:Name="toggleAll" Text="Hello"/>
</HorizontalStackLayout> it seems that
(the view model is indeed set by edit: It looks that my issue is explained here #23711 (comment). Still it would be great if it were working out of the box. |
So, what should I do with the current net9, keep the code as is and wait for #24238 to be merged?
I use quite a lot of x:References, and changing them all would be a disaster. |
@maonaoda right now, if you want to make sure that a binding is not compiled, set
You can then choose to ignore this warning by including the warning code in your project file: <WarningsNotAsErrors>$(WarningsNotAsErrors);XC0023</WarningsNotAsErrors> |
@simonrozsival However, when I simply add |
When the data type isn't known at compile time, the binding cannot be compiled. In that case, we produce a warning and fall back to a reflection-based binding the same way it is in .NET 8.
Hmm, you might also need to set |
I can repro this issue on the latest 17.12.0 Preview 1.0 (9.0.0-preview.7.24407.4) with the sample https://github.com/CommunityToolkit/Maui . and it does not repro on 9.0.0-preview.2.10293 |
Hi,
it works but i don't know how to set x:DataType correctly. The only thing helped is to set DataType to null: <DataTemplate x:DataType="{x:Null}">
<Button Text="{Binding Name}" Command="{Binding Source={RelativeSource Mode=FindAncestorBindingContext, AncestorType={x:Type vm:MyViewModel}}, Path=MyCommand, x:DataType=vm:MyViewModel}" />
</DataTemplate> |
It feels like this issue was taken for granted as "collateral damage", so that everyone could happily announce they finally reached trimming completeness |
@daltzctr @DDHSchmidt I opened a PR that would make the new compilation optimization that caused problems for you optional. Please feel free to leave feedback on the PR: #24924 |
Thanks, as I understood it enabling this switch will default back to the .Net8 mode of interpreting the Source-Bindings as before. That should help my case, yes. |
@DDHSchmidt thanks for the question. I'll try to summarize the problem and the possible fixes and workarounds.
Once #24924 is merged, the compiler will behave the same way it did in .NET 8. In other words, the optimization is NOT enabled by default. That should make it easier to adopt .NET 9 for you. If we can get the PR merged today, it will get into RC2 so you should be able to soon test it out. Thanks everyone for your feedback in this thread! The optimization will be enabled if you explicitly set a feature switch in your project file by adding
When the optimization is enabled, source-bindings are supported in .NET 9. For example, this scenario will be compiled just fine: <Entry x:Name="Name" />
<Label Text="{Binding Text, Source={x:Reference Name}}" x:DataType="Entry" /> There are just two potential issues you need to keep in mind when enabling it:
Please let me know if anything is unclear. |
Description
It's quite common to wire together XAML items together like the following
with the introduction of #20610, this broke as all bindings need to be compiled now. Great. Let's just declare a
DataType
as recommended by @simonrozsival.but oh no
Steps to Reproduce
Simply create a binding that binds to the property of another XAML item (such as
ContentView
), then try to explicitly declareDataType
Link to public reproduction project repository
No response
Version with bug
9.0.0-preview.6.24327.7
Is this a regression from previous behavior?
Yes, this used to work in .NET MAUI
Last version that worked well
9.0.0-preview.3.10457
Affected platforms
iOS, Android, Windows, macOS
Affected platform versions
No response
Did you find any workaround?
Explicitly declare null datatype inside of binding.
Relevant log output
No response
The text was updated successfully, but these errors were encountered: