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

Can't make many templated controls AoT compatible #10214

Open
dotMorten opened this issue Dec 4, 2024 · 2 comments
Open

Can't make many templated controls AoT compatible #10214

dotMorten opened this issue Dec 4, 2024 · 2 comments
Labels
area-AOT bug Something isn't working Regression team-Controls Issue for the Controls team

Comments

@dotMorten
Copy link
Contributor

dotMorten commented Dec 4, 2024

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
  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:

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.

@dotMorten dotMorten added the bug Something isn't working label Dec 4, 2024
@dotMorten dotMorten changed the title Near-impossible to make templated controls AoT compatible Can't make many templated controls AoT compatible Dec 4, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Dec 4, 2024
@ghost1372
Copy link
Contributor

@michael-hawker
Copy link
Collaborator

We've noticed similar issues with ElementName bindings as well in a control template, e.g. from the WCT ColorPicker:

 <controls:SwitchPresenter x:Name="ContentContainer" 
                           Grid.Row="2" 
                           Value="{Binding ElementName=ColorPanelSelector, Path=SelectedItem.Name}"> 

from CommunityToolkit/Windows#509 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-AOT bug Something isn't working Regression team-Controls Issue for the Controls team
Projects
None yet
Development

No branches or pull requests

4 participants