dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.1k stars 1.17k forks source link

In.NET 5.0 WPF, a window in a container cannot call a style resource in app.xaml #4538

Open lifuhua123 opened 3 years ago

lifuhua123 commented 3 years ago

In.NET 5.0 WPF, a window in a container cannot call a style resource in app.xaml , as shown in the figure : 1 2 3 4

miloush commented 3 years ago

This reproduces in 4.8.

This seems to be a bug in the compiler actually. When Application.StartupUri is not in App.xaml, the compiler does not generate and include app.baml in the assembly resources.

miloush commented 3 years ago

...which seems to be an intentional optimization:

https://github.com/dotnet/wpf/blob/89d172db0b7a192de720c6cfba5e28a1e7d46123/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/MarkupCompiler/ParserExtension.cs#L163-L180

miloush commented 3 years ago

@lifuhua123 and this code also hints a workaround: set any property on the Application element, for example explicitly include ShutdownMode="OnLastWindowClose".

ThomasGoulet73 commented 3 years ago

I think this issue is a duplicate of this one: https://github.com/dotnet/wpf/issues/4457. As I said in this comment: https://github.com/dotnet/wpf/issues/4457#issuecomment-827078499, the issue is caused by PresentationBuildTasks.dll so it will only apply to SDK-style csproj (.Net Framework or .Net Core) but this issue does not reproduce when using the old csproj style of .Net Framework.

miloush commented 3 years ago

This one reproduces for me with the old project file. That said, I agree this looks caused by the same underlying issue.

ThomasGoulet73 commented 3 years ago

@miloush You're right, just tested it again and I was able to reproduce this issue and #4457 using the old project style. I think I just forgot to remove StartupUri in my test.

miloush commented 3 years ago

@ryalanms I think this is meant to be a bug report rather than a question, but perhaps can be closed as duplicate.

lifuhua123 commented 3 years ago

@ThomasGoulet73 This problem is different from # 4457. Please look at the sample WpfApp2.zip

lifuhua123 commented 3 years ago

@miloush You're right, I added shutdownMode =" onLastWindowClose "and the program starts, but why?

ThomasGoulet73 commented 3 years ago

@lifuhua123 The problem is different but the cause is the same. You can also use the same workaround in #4457 of adding another style to your resource dictionary in App.xaml. The bug is that it should generate the InitializeComponent method even if there is only one Style in the resource dictionary. By adding a property, you trigger the generation of InitializeComponent that was already suppose to occur when there is on style.

lifuhua123 commented 3 years ago

Okay, so is this a problem or a bug? I tried another method, the program can also start. Set shutdownMode =" onLastWindowClose ", but don't know why? @ThomasGoulet73

ThomasGoulet73 commented 9 months ago

This bug should be fixed by dotnet/wpf#8853. For now, you can workaround this issue by adding a dummy item in <Application.Resources>.