microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.27k stars 674 forks source link

Enabling "PublishSingleFile" may causes "ResourceManager.Current" to throw an error #9836

Closed NXY666 closed 1 month ago

NXY666 commented 1 month ago

Describe the bug

When I tried to enable "PublishSingleFile" and attempted to publish it as a single executable file, an error occurred when accessing ResourceManager.Current:

Unable to find the specified file.
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32)
   at ABI.Windows.ApplicationModel.Resources.Core.IResourceManagerStaticsMethods.get_Current(IObjectReference)
   at Windows.ApplicationModel.Resources.Core.ResourceManager.get_Current()
   at Demo.MainWindow..ctor() in E:\Projects\Demo\MainWindow.xaml.cs:line 17

Steps to reproduce the bug

  1. Enable the PublishSingleFile option in win-x64.pubxml
  2. In the constructor of MainWindow, write the following code:

    public partial class MainWindow
    {
    public MainWindow()
    {
        string filePath = @"D:\demo.txt";
    
        try
        {
            var curr = ResourceManager.Current;
        } catch(Exception e)
        {
            File.WriteAllLines(filePath, [e.Message, e.StackTrace]);
        }
    }
    }
  3. Open this window on OnLaunched:
    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        MWindow = new MainWindow();
        MWindow.ExtendsContentIntoTitleBar = true;
        MWindow.Activate();
    }
  4. Publish, then run the exe and check the output errors in the root directory of the D drive:
    Unable to find the specified file.
    at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32)
    at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32)
    at ABI.Windows.ApplicationModel.Resources.Core.IResourceManagerStaticsMethods.get_Current(IObjectReference)
    at Windows.ApplicationModel.Resources.Core.ResourceManager.get_Current()
    at Demo.MainWindow..ctor() in E:\Projects\Demo\MainWindow.xaml.cs:line 17

Expected behavior

No response

Screenshots

No response

NuGet package version

Windows App SDK 1.5.5: 1.5.240627000

Packaging type

Unpackaged

Windows version

Windows 11 version 22H2 (22621, 2022 Update)

IDE

Visual Studio 2022

Additional context

This is the code mentioned above: Demo.zip

lhak commented 1 month ago

I think the ResourceManager from the Microsoft.Windows.ApplicationModel.Resources namespace will work with single file publishing.

NXY666 commented 1 month ago

I think the ResourceManager from the Microsoft.Windows.ApplicationModel.Resources namespace will work with single file publishing.

Thank you very much!

After replacing it with the ResourceManager you mentioned, it worked. I spent several months looking for a solution. 😂

Scottj1s commented 1 month ago

@NXY666 Thanks for reporting this, and @lhak thanks for the solution. Closing issue as resolved.