Skip to content

Setting Content on ContentControl crashes trimmed app #10426

@styris-ame

Description

@styris-ame

Describe the bug

After publishing a self contained, trimmed single file WinUI 3 executable, most things work normally. However, if you try to set the Content property on a ContentControl it will throw an exception:

[InvalidCastException] No such interface supported

int RoutedEventHandler.Do_Abi_Invoke(thisPtr, sender, e) >
RoutedEventHandler EventState.GetEventInvoke(sender, e) >
void PageViewer.OnApplyTemplate(sender, args) in PageViewer.cs:69 >
void PageViewer.SetCurrentPage(index) in PageViewer.cs:278 >
void ContentControl.set_Content(value) >
void IContentControlMethods.set_Content(_obj, value) >
void ExceptionHelpers.ThrowExceptionForHR(hr)

I assume the failure is due to IContentControlMethods getting trimmed.

Steps to reproduce the bug

Create a self-contained unpackaged WinUI app, and publish as trimmed and single file.

NuGet package version

Microsoft.WindowsAppSDK 1.6.250205002

1.7.250208002-preview1 has a similar error too:

[InvalidCastException] Specified cast is not valid.

int RoutedEventHandler.Do_Abi_Invoke(thisPtr, sender, e) >
RoutedEventHandler EventState.GetEventInvoke(sender, e) >
void PageViewer.OnApplyTemplate(sender, args) in PageViewer.cs:69 >
void PageViewer.SetCurrentPage(index) in PageViewer.cs:278 >
void ContentControl.set_Content(value) >
void IContentControlMethods.set_Content(_obj, value) >
void ExceptionHelpers.ThrowExceptionForHR(hr)

Windows version

Windows 11 24H2

Workaround

As a workaround, adding the following to the project csproj file solves the problem at the cost of adding ~7MB to the executable:

<ItemGroup>
    <TrimmerRootAssembly Include="Microsoft.WinUI"/>
</ItemGroup>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions