dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.39k stars 975 forks source link

Different Resource Names between NetFramework48 and Net8 #12267

Open CortiWins opened 1 week ago

CortiWins commented 1 week ago

.NET version

Net8.0-windows, Net Framework 4.8

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

While upgrading an application to multitarget of both Net8 and classic Net48, i encountered an issue. The icon on a form, (saved as a ressource in the forms resx) did load correctly on Net8 but caused an exception on net48.

Form.InitializeComponent()
...->
Icon = (Icon)resources.GetObject("$this.Icon");      
...->   
System.Resources.MissingManifestResourceException

Steps to reproduce

I tried to recreate the issue in an example problem but it did not happen, so i used ILSpy to check the ressources in the assembly of the application and noticed that they do have different names between the net48 assembly and the net8 assembly, and the difference is the namespace.

This allowed me to recreate the error in the example application.( see attachment ) MultitargetRessourceProblem.zip

In ILSpy it looks like this. As you can see, Net8 includes the Namespace into the resource name, while net4 does not. grafik

With a namespace matching the path in the project, both net48 and net8 can load the resource.

Tanya-Solyanik commented 5 days ago

@LeafShi1 - could your team please investigate a workaround, I guess we can define resources in the project file explicitly instead of relying on the designer generated code.

Epica3055 commented 4 days ago

We offer you two options

1.

Remove namespace (.FormsTest) in Form1.cs and Form1.designer.cs

2.

Add icon to the project. Image Change the code Image

CortiWins commented 3 days ago

During the last days i created a workaround for us that solves it for us for now. Doing that made me realize that my previous description of net8 including the namespace is not 100% specific.

net8 uses the namespace/formtypename net 48 uses projectname/foldername/formfilename Image

which leads us to two workarounds 1) making projectname, foldername and filename match exactly to namespace and typename. 2) set the form icon in code. from project ressources, from embeddedData or something else.

So as mentioned i made a workaround that works for us. So this is not a cry for help but just a plain old bug report, with a fancy image so not even that plain.

Have a nice day everyone!

LeafShi1 commented 3 days ago

This issue doesn't reproduce in separate NetFramework48 project or separate Net8 project, it only occurs when set multi target frameworks in a project <TargetFrameworks>net8.0-windows;net48</TargetFrameworks>

@Tanya-Solyanik This looks like a build issue, do we need to investigate this further?

Tanya-Solyanik commented 2 days ago

@merriemcgaw - multitargeting an important scenario to enable migration to .NET, should we move this issue to the designer repo and investigate further?

CortiWins commented 2 days ago

This issue doesn't reproduce in separate NetFramework48 project or separate Net8 project, it only occurs when set multi target frameworks in a project <TargetFrameworks>net8.0-windows;net48</TargetFrameworks>

From my tests, it is not the multitargeting. If i set the project that has the problem in multitarget to <TargetFramework>net48</TargetFramework> instead of multitarget, the probem still exists.

A new NetFramework48 project still uses the old project file format, while the problem happens in projects with a net SDK project file.

Tanya-Solyanik commented 1 day ago

@CortiWins - what happens when you change the .NET Framework project format to SDK style?

CortiWins commented 9 hours ago

@CortiWins - what happens when you change the .NET Framework project format to SDK style?

Net SDK style created with 8.0, TargetFramework changed to 4.8 -> error Net Framework 4.8 Updated via the Update Wizard to 8.0, TargetFramework changed to 4.8 -> error