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

A class library with a ResourceDictionary will crash with AOT/Trim enabled #10020

Closed
ghost1372 opened this issue Sep 18, 2024 · 4 comments
Closed
Labels
needs-triage Issue needs to be triaged by the area owners

Comments

@ghost1372
Copy link
Contributor

ghost1372 commented Sep 18, 2024

Describe the bug

I have a class library that contains some ResourceDictionaries which i refrence them into app.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
            <ResourceDictionary Source="ms-appx:///WinUICommunity.Components/Themes/Generic.xaml" />
            <ResourceDictionary Source="ms-appx:///WinUICommunity.LandingPages/Themes/Generic.xaml" />
            <ItemTemplates xmlns="using:WinUICommunity" />
        </ResourceDictionary.MergedDictionaries>
        <!--  Other app resources here  -->
    </ResourceDictionary>
</Application.Resources>

, When I activate the PublishAOT or Trim switch:

<PublishAOT>true</PublishAOT>
<PublishTrimmed>true</PublishTrimmed>

My app does not run in debug or release or publish mode at all.
In debug mode, I get the following exception

  Name Value Type
  Message "Failed to assign to property 'Microsoft.UI.Xaml.ResourceDictionary.Source' because the type 'Windows.Foundation.String' cannot be assigned to the type 'Windows.Foundation.Uri'. [Line: 10 Position: 37]" string
Exception {"The text associated with this error code could not be found.\r\n\r\nFailed to assign to property 'Microsoft.UI.Xaml.ResourceDictionary.Source' because the type 'Windows.Foundation.String' cannot be assigned to the type 'Windows.Foundation.Uri'. [Line: 10 Position: 37]"} System.Exception {Microsoft.UI.Xaml.Markup.XamlParseException}

after removing my resource dictionary refrences from app.xaml My app runs without problems.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

I also tried this with a new class library and an empty resource dictionary and issue exist

Steps to reproduce the bug

1.Create a New WinUI 3 Project
2.Make sure your BuildTools is 22621.3232 (latest build tools can not be launch in AOT)
3.Create a Class Library with a Resource Dictionary (Create a nuget package)
4.Add Class Library to Project
5.Add Resource Dictionary in App.xaml
6.Run app (Works fine)
7.Add <PublishAOT>true</PublishAOT> or <PublishTrimmed>true</PublishTrimmed>
8.Try to run app (app will be crash)

Expected behavior

App Should be Run/Launch without any issues

Screenshots

No response

NuGet package version

Windows App SDK 1.6.0: 1.6.240829007

Packaging type

Packaged (MSIX), Unpackaged

Windows version

Windows 11 version 22H2 (22621, 2022 Update)

IDE

Visual Studio 2022-preview, Visual Studio 2022

Additional context

No response

@ghost1372
Copy link
Contributor Author

i found that this is related to Trim and i uploaded a Repro sample. @codendone Is there any news about fixing this bug? Actually, I can't publish any app

App1-CrashRC.zip

Open CsProj file and make sure Trim is enabled in Debug/Release

<PublishTrimmed Condition="'$(Configuration)' == 'Debug'">True</PublishTrimmed>
<PublishTrimmed Condition="'$(Configuration)' != 'Debug'">True</PublishTrimmed>

now try launch app, and you can see app will be crash.
Image

@ghost1372 ghost1372 changed the title A class library with a ResourceDictionary will crash with AOT enabled A class library with a ResourceDictionary will crash with AOT/Trim enabled Oct 10, 2024
@lhak
Copy link

lhak commented Oct 13, 2024

I tested your repro project and it works correctly here. Make sure you use an up-to-date .net installation so that the cswinrt source generator runs.

@ghost1372
Copy link
Contributor Author

I tested your repro project and it works correctly here. Make sure you use an up-to-date .net installation so that the cswinrt source generator runs.

Thank you.
installing CSWinRT nuget package fixed issue. i am using Visual Studio 2022 Preview.

@ghost1372
Copy link
Contributor Author

I fixed all the crashes by installing Microsoft.Windows.CsWinRT package in the ClassLibrary project and the app itself.
so i am closing this issue

@microsoft-github-policy-service microsoft-github-policy-service bot added needs-triage Issue needs to be triaged by the area owners and removed needs-triage Issue needs to be triaged by the area owners labels Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Issue needs to be triaged by the area owners
Projects
None yet
Development

No branches or pull requests

2 participants