Watch this in action on YouTube: https://youtu.be/xPI8hLpyc5k
This sample app demonstrates the new XAML C# Expressions feature coming to .NET MAUI - allowing you to embed C# expressions directly in XAML property values.
⚠️ Preview Warning: This feature is experimental and planned for .NET 11. The API and syntax may change before release. This demo uses a custom preview build of .NET MAUI on .NET 10 and should not be used in production.
XAML C# Expressions is a new XAML Source Generator feature that lets you write C# code directly in your XAML, eliminating the need for many common converters and code-behind event handlers.
<!-- NEW: Implicit syntax -->
<Label Text="{Name}" />
<!-- NEW: Explicit syntax (with = prefix) -->
<Label Text="{= Name}" />
<!-- OLD: Traditional binding -->
<Label Text="{Binding Name}" /><!-- NEW -->
<Label Text="{$'Hello, {Name}!'}" />
<!-- OLD -->
<Label Text="{Binding Name, StringFormat='Hello, {0}!'}" /><!-- NEW -->
<Label Text="{$'Total: ${Price * Quantity:F2}'}" />
<!-- OLD: Required a computed property or MultiBinding with converter -->
<Label Text="{Binding Total, StringFormat='Total: ${0:F2}'}" /><!-- NEW -->
<Label IsVisible="{!IsHidden}" />
<!-- OLD: Required BooleanInvertConverter -->
<Label IsVisible="{Binding IsHidden, Converter={StaticResource BooleanInvertConverter}}" /><!-- NEW -->
<Button IsEnabled="{HasAccount && AgreedToTerms}" />
<!-- OLD: Required MultiBinding with AllTrueConverter -->
<Button>
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource AllTrueConverter}">
<Binding Path="HasAccount" />
<Binding Path="AgreedToTerms" />
</MultiBinding>
</Button.IsEnabled>
</Button><!-- NEW -->
<Button Clicked="{(s, e) => this.OnCounterClicked()}" />
<!-- OLD -->
<Button Clicked="OnCounterClicked" /><!-- NEW -->
<Button Clicked="{async (s, e) => await SaveAsync()}" />
<!-- OLD: Required async void event handler in code-behind -->
<Button Clicked="OnSaveClicked" />To enable XAML C# Expressions, add the following to your .csproj:
<PropertyGroup>
<MauiXamlInflator>SourceGen</MauiXamlInflator>
</PropertyGroup>You also need x:DataType set on your XAML page for the expression resolver to work:
<ContentPage x:DataType="local:MainViewModel">| Syntax | Description | Example |
|---|---|---|
{Property} |
Implicit binding to x:DataType | {Name} |
{= Property} |
Explicit expression syntax | {= Name} |
{$'...'} |
String interpolation | {$'Hello {Name}'} |
{!Bool} |
Boolean negation | {!IsHidden} |
{A && B} |
Boolean AND | {HasAccount && AgreedToTerms} |
{A || B} |
Boolean OR | {IsAdmin || IsOwner} |
{A * B} |
Arithmetic expressions | {Price * Quantity} |
{(s, e) => ...} |
Lambda event handler | {(s, e) => Count++} |
{async (s, e) => ...} |
Async lambda handler | {async (s, e) => await LoadAsync()} |
{this.Property} |
Explicit page member | {this.Title} |
{.Property} |
Explicit binding context | {.Name} |
The XAML Source Generator:
- Parses expressions at compile time using Roslyn
- Auto-detects whether identifiers belong to
this(page) orx:DataType(ViewModel) - Generates strongly-typed bindings or direct property access
- Creates lambda wrappers for event handlers
This repository contains a preview/experimental feature built from a PR branch. It is:
- ❌ Not production ready
- ❌ Subject to breaking changes
- ❌ Using unofficial NuGet packages
- ✅ For demonstration and feedback purposes only
This sample is provided as-is for educational purposes.