Open jnm2 opened 3 years ago
You need to add a ResourceDictionary
to contain your Style and make it to the Application.Current.Resources.Source
in App.xaml.cs, eg, You can put your style into a ResourceDictionary
named a Dictionary1
, and add below code to your App.xaml.cs
public App()
{
Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("/ApplicationStyles;Component/Dictionary1.xaml",UriKind.Relative)) as ResourceDictionary);
}
Why is this needed if it's not needed when StartupUri is used and it's not needed if there is more than one style?
This bug looks really weird. It's from the code generated by PresentationBuildTasks.dll.
See the difference between the generated code in App.g.i.cs:
It does not seem to generate the InitializeComponent when there is only one item in <Resources>
.
My guess would be that there is a wrong condition here that prevents the generation of InitializeComponent: https://github.com/dotnet/wpf/blob/main/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/MarkupCompiler/MarkupCompiler.cs#L3083
This bug should be fixed by dotnet/wpf#8853. For now, you can workaround this issue by adding a dummy item in <Application.Resources>
.
The application style has no effect, but it should have an effect. The style starts taking effect as soon as a second unrelated style is added before or after it.
This causes a long search for ways to fix the problem and is not beginner-friendly.
Full repro created from VS template
Download: Repro.zip