views:

584

answers:

3

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.

+2  A: 

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
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
A: 

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
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
A: 

Thank you so much. Saved me a lot of time.

If your AssemblyInfo.cs is newly created, then you need to add "using System.Windows;"

thanks

John

John Radley