Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Resources in class library in .NET 5

Before, in .NET Framework, when I created a WPF class library, I had my App.xaml (set as Application definition) referencing my resource dictionaries like that:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ResourceDictionary1.xaml" />
                <ResourceDictionary Source="ResourceDictionary2.xaml" />
                <ResourceDictionary Source="ResourceDictionary3.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

However, now, with .NET 5, I get this compilation error:

Library project file cannot specify ApplicationDefinition element.

So that message means I can't have an App.xaml file in a class library anymore. But now, I don't know how to define my resources globally in a class library. Isn't there any way to do this without referencing the dictionaries in each and every XAML file in the project?

Edit: It's not about the Source syntax. The path is correct and everything compiles perfectly if I set the project to Windows Application instead of Class Library.

like image 754
krimog Avatar asked Sep 10 '25 15:09

krimog


1 Answers

refering to that github issue : https://github.com/dotnet/wpf/issues/2812#issuecomment-607537794

In an SDK style WindowsDesktop project,

*.xaml are all treated as Page items by default, unless they are named App.xaml (C#) or Application.xaml (VB).

App.xaml/Application.xaml is treated as ApplicationDefinition by default.

<!-- 
Disables automatic globbing of App.xaml/Application.xaml into 
ApplicationDefinition item
App.xaml/Application.xaml would now get treated as any other *.xaml file 
(typically, a Page)
--> 
<PropertyGroup>
  <EnableDefaultApplicationDefinition>false</EnableDefaultApplicationDefinition>
</PropertyGroup>

Worth a try.

like image 128
Xendre Xendre Avatar answered Sep 13 '25 06:09

Xendre Xendre