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

WinUI 3 DataTemplateSelector with x:Bind (x:DataType) works in Debug but crashes in Release #10310

Closed
FoxitJohnny opened this issue Jan 18, 2025 · 2 comments
Labels
bug Something isn't working needs-author-feedback Asked author to supply more information.

Comments

@FoxitJohnny
Copy link

FoxitJohnny commented Jan 18, 2025

Describe the bug

I have a WinUI 3 app with a DataTemplateSelector and typed DataTemplates that use x:Bind and x:DataType="MyNamespace.Setting". Everything works perfectly in Debug mode, but in Release mode the app crashes with a NullReferenceException (raised from external code, so I don’t get a clear stack trace).

Steps to reproduce the bug

  • I have a ListView bound to an ObservableCollection<Setting>.
  • Inside the ListView.ItemTemplate, I use a <ContentControl ContentTemplateSelector="{StaticResource MySelector}" Content="{x:Bind}" />.
  • My DataTemplateSelector overrides SelectTemplateCore(object item, DependencyObject container), returning one of two typed data templates:
<DataTemplate x:Key="StringTemplate" x:DataType="models:Setting">
    <StackPanel>
        <TextBlock Text="{x:Bind Name}" />
        <TextBox Text="{x:Bind Value, Mode=TwoWay}" />
    </StackPanel>
</DataTemplate>

<DataTemplate x:Key="NumberTemplate" x:DataType="models:Setting">
    <StackPanel>
        <TextBlock Text="{x:Bind Name}" />
        <TextBox Text="{x:Bind Value, Mode=TwoWay}" />
    </StackPanel>
</DataTemplate>
  • My model is:
public enum SettingDataType { String, Number }

public class Setting
{
    public string Name { get; set; }
    public object Value { get; set; }
    public SettingDataType DataType { get; set; }
}

Expected behavior

When using WinUI 3 with typed DataTemplates (x:DataType="models:Setting") and a DataTemplateSelector, the same code and XAML that work in Debug should also run successfully in Release. Specifically:

  • SelectTemplateCore is called for each item to choose the correct DataTemplate at runtime.
  • The typed DataTemplate (with x:DataType and x:Bind) should be compiled without errors or missing references in both Debug and Release configurations.
  • The app should not crash or throw a NullReferenceException when loading the templates in Release.

In other words, the linker and runtime should preserve all necessary types, properties, and references so the UI renders properly without any special build-time or runtime differences between Debug and Release.

Screenshots

Running in Debug:
Image

Running in Release:
Image

TestBinding2.zip

NuGet package version

None

Windows version

No response

Additional context

No response

@FoxitJohnny FoxitJohnny added the bug Something isn't working label Jan 18, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jan 18, 2025
@kmgallahan
Copy link
Contributor

Probably dupe of #10302

@marcelwgn
Copy link
Collaborator

Thanks for linking to the other issue @kmgallahan !

@FoxitJohnny Have you checked your app has any CsWinrt-AOT warnings by adding <CsWinRTAotWarningLevel>2</CsWinRTAotWarningLevel> to the csproj file?

@marcelwgn marcelwgn added needs-author-feedback Asked author to supply more information. and removed needs-triage Issue needs to be triaged by the area owners labels Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-author-feedback Asked author to supply more information.
Projects
None yet
Development

No branches or pull requests

3 participants