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

Compiled Bindings in Code Are No Longer Available and Documentation Update Needed #2729

Closed
WolfgangPhy opened this issue Jan 13, 2025 · 8 comments
Labels
dotnet-maui/subsvc dotnet-mobile/svc Pri1 High priority, do before Pri2 and Pri3 resolved-by-customer Indicates issues where the customer no longer needs any help. [org]

Comments

@WolfgangPhy
Copy link

WolfgangPhy commented Jan 13, 2025

Type of issue

Typo

Description

Based on the following code snippet:

Source

It appears that compiled bindings in code are no longer supported or functional. This change affects how bindings are utilized programmatically.

As a result, the related class documentation also needs to be updated to reflect this change:

Class documentation

This discrepancy between the code behavior and the documentation might lead to confusion for developers relying on this feature.

Page URL

https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/compiled-bindings?view=net-maui-9.0

Content source URL

https://github.com/dotnet/docs-maui/blob/main/docs/fundamentals/data-binding/compiled-bindings.md

Document Version Independent Id

d8079bc8-3987-9069-717e-c5cbf3512ae1

Article author

@davidbritch

Metadata

  • ID: d8079bc8-3987-9069-717e-c5cbf3512ae1
  • Service: dotnet-mobile
  • Sub-service: dotnet-maui

Related Issues

@issues-automation issues-automation bot added dotnet-maui/subsvc dotnet-mobile/svc Pri1 High priority, do before Pri2 and Pri3 labels Jan 13, 2025
@dotnetrepoman dotnetrepoman bot added the ⌚ Not Triaged Not triaged label Jan 13, 2025
@davidbritch
Copy link
Contributor

Hi @WolfgangPhy

Compiled bindings in code are still supported. What happens is that a source generator creates a SetBinding method that accepts a Func at build time, and it's this generated method that's used to support compiled bindings in code. If you want to see this in action, add `true into your .csproj and you'll be able to see the C# code that gets generated. IIRC it goes to obj/generated (or somewhere like that). For example:

Image

@WolfgangPhy
Copy link
Author

Hi @davidbritch,

Thank you for your quick response and for clarifying how the source generator works to create the SetBinding method at build time. I understand that this process supports compiled bindings in code, and I appreciate you pointing me to the generated code location.

However, I’d like to confirm something: does using SetBinding in a basic example like this:

_confirmationTitleView.SetBinding(ConfirmationTitleViewBase.TitleProperty, new Binding(CollectionNamePath, mode: BindingMode.TwoWay));

automatically generate a compiled binding?

I ask because the version shown in the documentation, such as:

MyLabel.SetBinding(Label.TextProperty, static (Entry entry) => entry.Text);

doesn’t seem to work as expected. Given this, is my issue about the documentation being outdated still valid?

Thank you again for your time and help!

@davidbritch
Copy link
Contributor

does using SetBinding in a basic example like this:

_confirmationTitleView.SetBinding(ConfirmationTitleViewBase.TitleProperty, new Binding(CollectionNamePath, mode: BindingMode.TwoWay));

automatically generate a compiled binding?

No. It only generates a compiled binding if you use a Func. To turn this into a compiled binding you'd need to replace new Binding(...) with Binding.Create(...) and specify a Func as the appropriate argument.

Given this, is my issue about the documentation being outdated still valid?

The page about compiled bindings is accurate. However, most of the other docs are not yet using compiled bindings. In December I went through all of the MAUI samples (in dotnet/maui-samples) and replaced the bindings with compiled bindings. The next job is to go through all the docs and ensure that every binding expression uses a compiled binding (see the issues at https://github.com/dotnet/docs-maui/issues?q=is%3Aissue%20state%3Aopen%20compiled-bindings).

@WolfgangPhy
Copy link
Author

The next job is to go through all the docs and ensure that every binding expression uses a compiled binding (see the issues at

I genuinely have great respect for your work, which is incredibly important and necessary. Knowing that someone is actively addressing this is truly encouraging. I wish you the best of luck in your future endeavors.


Regarding the solution you provided, does it only work with .NET 9? It seems that the Binding.Create method doesn't exist in my current setup.

@davidbritch
Copy link
Contributor

Regarding the solution you provided, does it only work with .NET 9? It seems that the Binding.Create method doesn't exist in my current setup.

Yes. .NET 9 only.

@WolfgangPhy
Copy link
Author

So there is no Compiled Bindings in code for.NET 8 ?

@davidbritch
Copy link
Contributor

Correct. In .NET 8 there are compiled bindings in XAML, but not in code. .NET 9 has forced the issue due to the introduction of the trimmer, which will remove string-based bindings from your app (when full trimming is used).

@WolfgangPhy
Copy link
Author

Ok thank you ! So i close this issue !

@dotnetrepoman dotnetrepoman bot added resolved-by-customer Indicates issues where the customer no longer needs any help. [org] and removed ⌚ Not Triaged Not triaged labels Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-maui/subsvc dotnet-mobile/svc Pri1 High priority, do before Pri2 and Pri3 resolved-by-customer Indicates issues where the customer no longer needs any help. [org]
Projects
None yet
Development

No branches or pull requests

2 participants