Skip to content

.NET MAUI 9: Compiled bindings #2427

@davidbritch

Description

@davidbritch

In .NET MAUI 9 there's a source generator that allows developers to call the SetBinding extension method to define bindings on their bindable objects using Func instead of string paths. There's also an additional API that builds on this that will not only set the binding directly to the object, but it will return the binding object instance and allow the developer to pass it to some other method and use it later.

This approach is required instead of string-based bindings in NativeAOT apps, and in apps with full trimming enabled.

String-based binding in code:

label.BindingContext = new PageViewModel { Customer = new CustomerViewModel { Name = "John" } };
label.SetBinding(Label.TextProperty, "Customer.Name");

Compiled binding in code:

label.SetBinding<PageViewModel, string>(Label.TextProperty, static vm => vm.Customer.Name);
// or with type inference:
label.SetBinding(Label.TextProperty, static (PageViewModel vm) => vm.Customer.Name);

Compiled binding in XAML:

<Label Text="{Binding Customer.Name}" x:DataType="local:PageViewModel" />

Will also need to update the compiled bindings doc.

More info: dotnet/maui#23239


Associated WorkItem - 314392

Metadata

Metadata

Assignees

Labels

🏁 Release: .NET 10Work items for the .NET 10 release📌 seQUESTeredIdentifies that an issue has been imported into Quest.doc-enhancementImprove the current content [org]resolved-by-customerIndicates issues where the customer no longer needs any help. [org]

Type

No type
No fields configured for issues without a type.

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions