20

If I have a ResourceDictionary in one project, is it possible to create another project that uses resources defined in the first project? Note that both projects are WPF Applications, not ControlLibraries.

Thanks!!

Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149

2 Answers2

32

Yes, of course that's possible, as long as Project B has a reference to Project A.

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Project A;component/YourSubFolder/YourResourceFile.xaml" />
</ResourceDictionary.MergedDictionaries>

Then you can just use the Resources defined in YourResourceFile.xaml.

Kenan E. K.
  • 13,955
  • 3
  • 43
  • 48
user112889
  • 805
  • 9
  • 15
  • Oh snap. I'm giving that a try! Thanks!! – Mark Carpenter Jun 03 '09 at 20:32
  • What happens if the projects don't share a reference? In my case I'm using PRISM and declaring styles in the Shell. I'd like to be able to reuse some of the defined colours. – R4cOOn Jul 31 '09 at 13:07
  • If the Styles are defined in the Shell, then every object can use them, I think. Since the XAML is loaded into the MainPage at runtime, it also gets access to the global Resources. – user112889 Aug 02 '09 at 22:44
13

I found that I had to reference the assembly itself and not use a project name. I also did not need to use the pack:/// syntax to get this to work.

This answer on the duplicate question specifies the format to use (I can verify that this syntax works in .NET 4.0): https://stackoverflow.com/a/10216253/1260563

Specifically (since I always forget the component part thinking it is a folder someone is using):

<ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source="/<YourAssemblyName>;component/<YourReferencedFileHere.xaml>" />
</ResourceDictionary.MergedDictionaries>

So if you have an assembly Abc.Def.dll and a file in that DLL called Xyz.xaml at the root level you would use:

<ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source="/Abc.Def;component/Xyz.xaml" />
</ResourceDictionary.MergedDictionaries>

Note: Resharper 7 pointed out that I had to reference the assembly itself.

Community
  • 1
  • 1
David Dowdle
  • 423
  • 6
  • 10
  • Thank you for giving specific examples and pointing out the need for the component part of the path. This helped more than the half-dozen other solutions I've stumbled across. – Pete Magsig Jul 09 '16 at 12:27