7

I posted a question a few months ago about sharing resource dictionaries across assemblies. It turns out you can do that using the Component Resource Key markup extension. At the time, I could only get it working with a WPF Custom Control project, not with a plain Class Library project.

Now I need to use an existing plain Class Library project to host a shared resource dictionary. That means I need to retrofit the Class Library project to support the Component Resource Key markup extension. I have added a Themes folder and a Generic.xaml resource dictionary document to the Class Library project, as well as references to PresentationCore, PresentationFramework, and WindowsBase. Unfortunately, that doesn't seem to do the trick.

So, here is my question: Other than the above, what does a WPF Custom Control Library project have that a plain Class Library project doesn't? Or, to put it another way, what else could I add to my class library project to get this feature working? Thanks.

cdiggins
  • 17,602
  • 7
  • 105
  • 102
David Veeneman
  • 18,912
  • 32
  • 122
  • 187
  • The Component Resource Key markup extension isn't working. I set up two identical solutions, one with a Custom Control Library, and the other with a plain Class Library with the DLL refs and Themes/Generic.xaml. The Component Resource Key markup extension works in the Custom Control Library, but not in the Class Library. – David Veeneman Jan 18 '10 at 22:31
  • I have worked around the problem by replacing the old Class Library with a Custom Control Library and moving all of the classes from the Class Library to the Custom Control Library. But I would still like to figure out how to retrofit a Class Library, because I will probably come across this again. – David Veeneman Jan 18 '10 at 22:34
  • Weird. The following worked for me: create a class library project, add WPF DLL references, add a Themes/Generic.xaml (no Resource Dictionary option so created as UserControl and edited XAML to be a ResourceDictionary), created a CRK in a class (based on your linked question), added a resource to generic.xaml with x:Key set to that CRK. Compiles fine for me (VS2008 SP1). I didn't need the ThemeInfoAttribute that Cameron mentions. Might be worth posting your XAML file? – itowlson Jan 19 '10 at 06:27
  • Also useful could be the following: https://stackoverflow.com/a/70308458/184528 – cdiggins Dec 10 '21 at 17:51

3 Answers3

7

Apart from the extra WPF references, the WPF Custom Control Library template has an extra attribute in AssemblyInfo.

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

ThemeInfoAttribute specifies the location in which theme dictionaries are stored for types in an assembly.

Cameron MacFarland
  • 70,676
  • 20
  • 104
  • 133
  • Note that I haven't tested this solution, but it certainly makes sense. I would expect the lack of that attribute to diable Themes/generic.xaml support. – David Veeneman Jan 19 '10 at 14:03
2

Cameron MacFarland's answer was spot on. I have now tested it, and it works.

Here is the solution: Add the DLL refs and the Themes/generic.xaml file to the plain Class Library project. Then, open AssemblyInfo.cs and add the following code at the end of the file:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

Recompile, and the Component Resource Key markup extension should work.

David Veeneman
  • 18,912
  • 32
  • 122
  • 187
  • I found a simpler solution to cross-assembly resource sharing, using Pack URIs instead of ComponentResourceKeys. The approach is documented here: http://stackoverflow.com/questions/2095031/wpf-sharing-resources-across-assemblies – David Veeneman Jan 19 '10 at 21:19
2

Another difference is in the .csproj File:

In class Library the Tag is missing:

<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

After adding it to the first PropertyGroup, the add menu of the project shows now the typical WPF files.

Markus k
  • 513
  • 1
  • 4
  • 8