microsoft / microsoft/microsoft-ui-xaml

Can't make many templated controls AoT compatible

Open
#10,214 5 comments 26 reactions 0 assignees View on GitHub
area-AOT bug Regression team-Controls
Dominant language
C++
Stars
8.4k
Forks
942
Avg merge
2d 7h
Merged PRs (30d)
105

Description

### Describe the bug

If you're building custom controls, it is VERY hard to make them AoT compatible, because the control templates can no longer use Binding, and you can't use x:DataType.
Say you have a control that provides a default ItemTemplate, this item template must use x:Bind to work with AoT, but you can't use x:Bind in a resource template.
Trying to do so will give you the following error:

> 1>E:\source\Themes\Generic.xaml(21,36): XamlCompiler error WMC1119: This Xaml file must have a code-behind class to use {x:Bind}. See http://go.microsoft.com/fwlink/?LinkID=532920&clcid=0x409 for more information

Not using x:DataType will cause the app to crash when publishing trimmed because the datatype isn't preserved and setting the ItemSource will crash (with the unhelpful `System.ArgumentException: 'Value does not fall within the expected range.'` message on the ItemsSource setter.

Another issue that often is hit is getting access to a sub-property from template binding, that isn't possible. There's really no way to convert this classic binding expression:
`Text="{Binding SomeProperty.SubProperty, RelativeSource={RelativeSource TemplatedParent}}"` since you can't replace it with `Text="{TemplateBinding SomeProperty.SubProperty}"`

It feels to me that there's a major thing missing from the AoT story when it comes to custom control templating.

### Steps to reproduce the bug

1. Unzip the following sample app: [AoTWoes.zip](https://github.com/user-attachments/files/18014888/AoTWoes.zip)
2. Try and compile
3. Observe the `WMC1119` xaml compiler error.
4. In `Themes\Generic.xaml` remove the default item template and include the commented-out `{Binding}` based item template.
5. Run the app and observe runtime-crash
6. Disable PublishAoT and notice app runs just fine.

### Expected behavior

A way to use x:Bind in control templates that is AoT compatible.

### Screenshots

_No response_

### NuGet package version

WinUI 3 - Windows App SDK 1.6.3: 1.6.241114003

### Windows version

Windows 11 (24H2): Build 26100

### Additional context

I also tried adding a codebehind file to Generic.xaml like this:
```cs
using Microsoft.UI.Xaml;

namespace AoTWoes;

public partial class ThemeResources : ResourceDictionary
{
public ThemeResources()
{
InitializeComponent();
}
}
```
And set the `x:Class` property on `Generic.xaml`. That gets past the issue of being able to use `x:DataType` on the data template, however it still doesn't work at runtime. It also has the downside of introducing public classes that really shouldn't be exposed to the end user.

Contributor guide

Open the contributing guide

Research direction

Start with the AoTWoes.zip sample and Themes\Generic.xaml, then reproduce the WMC1119 error while compiling with PublishAoT enabled. Compare the x:Bind and Binding-based item templates and verify behavior when the app is published trimmed. Done means custom control templates can use an AoT-compatible binding approach without the reported compile error or runtime crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop, frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.