251

I have resource dictionary files (MenuTemplate.xaml, ButtonTemplate.xaml, etc) that I want to use in multiple separate applications. I could add them to the applications' assemblies, but it's better if I compile these resources in one single assembly and have my applications reference it, right?

After the resource assembly is built, how can I reference it in the App.xaml of my applications? Currently I use ResourceDictionary.MergedDictionaries to merge the individual dictionary files. If I have them in an assembly, how can I reference them in xaml?

CharithJ
  • 42,659
  • 20
  • 104
  • 124
Gus Cavalcanti
  • 10,127
  • 22
  • 68
  • 102
  • 1
    This may be an overkill, but you may want to prepare your resources for Export using the technique described here: https://alexfeinberg.wordpress.com/2015/08/16/safely-export-wpf-resources/. The main advantage of doing this is to prevent problems with multiple versions of the resource assembly getting loaded into same appdomain. – user195275 Aug 16 '15 at 17:28

6 Answers6

366

Check out the pack URI syntax. You want something like this:

<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Subfolder/YourResourceFile.xaml"/>
Tim Cooper
  • 144,163
  • 35
  • 302
  • 261
Kent Boogaart
  • 165,446
  • 34
  • 376
  • 376
  • 1
    What if YourAssembly is not contained inside the application path? – SaneDeveloper Aug 26 '13 at 10:10
  • @Engineer Spock: then the CLR won't find it without help (nothing specifically to do with WPF). Either add probing paths to your *app.config*, or attach to `AppDomain.AssemblyResolve` to help it find the assembly. – Kent Boogaart Aug 26 '13 at 10:22
  • 1
    Do I need to add probing path if YourAssembly project is at the same level as the application project that need to reference YourAssembly? For instance, C:\Solution\AppProject\ and C:\Solution\YourAssemblyProject\ – SaneDeveloper Aug 26 '13 at 10:40
  • 1
    @EngineerSpock: this is a separate question, so please open one. – Kent Boogaart Aug 26 '13 at 10:52
  • Have already opened http://stackoverflow.com/questions/18439516/how-to-reference-an-assemly-residing-in-relative-path-using-uri-pack-syntax/18439679?noredirect=1#comment27095370_18439679 – SaneDeveloper Aug 26 '13 at 11:18
  • A more specific link to the exact section describing this syntax: http://msdn.microsoft.com/en-us/library/aa970069.aspx#Resource_File_Pack_URIs___Referenced_Assembly – Austin Mullins Aug 01 '14 at 14:14
  • 4
    This answer makes no sense. In order to follow it one already needs to know how to do it! – user1040323 Oct 16 '18 at 15:20
99

An example, just to make this a 15 seconds answer -

Say you have "styles.xaml" in a WPF library named "common" and you want to use it from your main application project:

  1. Add a reference from the main project to "common" project
  2. Your app.xaml should contain:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Common;component/styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

GEOCHET
  • 20,623
  • 15
  • 71
  • 98
Hertzel Guinness
  • 5,642
  • 2
  • 35
  • 43
  • 3
    And then how do you make the resources defined in styles.xaml available via the Visual Studio 2010 Properties window? If I select an element, and then click Apply Resource for it's Background property, it only shows SystemColors and not those defined in styles.xaml. But if I type the resource name in XAML myself it works, so it is correctly referenced. – xr280xr May 13 '13 at 18:42
  • Just wanted to add that if you reference the ResourceDictionary from UserControl, then you need to add a reference to the assembly in both places: in the UserControl and in the main window project. Otherwise you'll get the runtime error. – Andrejs Gasilovs Jun 12 '17 at 09:31
39

I'm working with .NET 4.5 and couldn't get this working... I was using WPF Custom Control Library. This worked for me in the end...

<ResourceDictionary Source="/MyAssembly;component/mytheme.xaml" />

source: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/11a42336-8d87-4656-91a3-275413d3cc19

Aleksandar Toplek
  • 2,471
  • 26
  • 41
21

Resource-Only DLL is an option for you. But it is not required necessarily unless you want to modify resources without recompiling applications. Have just one common ResourceDictionary file is also an option. It depends how often you change resources and etc.

<ResourceDictionary Source="pack://application:,,,/
     <MyAssembly>;component/<FolderStructureInAssembly>/<ResourceFile.xaml>"/>

MyAssembly - Just assembly name without extension

FolderStructureInAssembly - If your resources are in a folde, specify folder structure

When you are doing this it's better to aware of siteOfOrigin as well.

WPF supports two authorities: application:/// and siteoforigin:///. The application:/// authority identifies application data files that are known at compile time, including resource and content files. The siteoforigin:/// authority identifies site of origin files. The scope of each authority is shown in the following figure.

enter image description here

CharithJ
  • 42,659
  • 20
  • 104
  • 124
8

For UWP:

<ResourceDictionary Source="ms-appx:///##Namespace.External.Assembly##/##FOLDER##/##FILE##.xaml" />
Gianluca Demarinis
  • 1,666
  • 2
  • 11
  • 15
5

Using XAML:

If you know the other assembly structure and want the resources in c# code, then use below code:

 ResourceDictionary dictionary = new ResourceDictionary();
 dictionary.Source = new Uri("pack://application:,,,/WpfControlLibrary1;Component/RD1.xaml", UriKind.Absolute);
 foreach (var item in dictionary.Values)
 {
    //operations
 }

Output: If we want to use ResourceDictionary RD1.xaml of Project WpfControlLibrary1 into StackOverflowApp project.

Structure of Projects:

Structure of Projects

Resource Dictionary: Resource Dictionary

Code Output:

Output

PS: All ResourceDictionary Files should have Build Action as 'Resource' or 'Page'.

Using C#:

If anyone wants the solution in purely c# code then see my this solution.

Community
  • 1
  • 1
Kylo Ren
  • 7,647
  • 4
  • 36
  • 57