ResourceDictionary in a separate assembly

148,097

Solution 1

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

<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Subfolder/YourResourceFile.xaml"/>

Solution 2

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>

Solution 3

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

Solution 4

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

Solution 5

For UWP:

<ResourceDictionary Source="ms-appx:///##Namespace.External.Assembly##/##FOLDER##/##FILE##.xaml" />
Share:
148,097

Related videos on Youtube

Christopher Mann
Author by

Christopher Mann

Veteran software engineer and database architect. With more than 15 years of experience I have built solutions for a wide variety of organizations; from federal government agencies in Brazil to one of the largest health care systems in the United States. My specialties are .Net, SQL Server, Business Analytics, and user experience in general. Occasional speaker in code camps and user groups, member of the International .Net Association, president/founder of the Central California .Net User Group, Central California Give Camp, board member of the Central Valley Software Partnership in Central California, member of the International Institute of Business Analysis. I have a Bachelor's Degree in Computer Science and an MBA with emphasis in Information Systems and Entrepreneurship from California State University, Fresno. Microsoft Certified IT Professional (MCITP) in SQL Server Programming and Administration, Microsoft Certified Professional in C#, Certified Qlikview Developer and Designer.

Updated on January 28, 2022

Comments

  • Christopher Mann
    Christopher Mann over 2 years

    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?

    • user195275
      user195275 almost 9 years
      This may be an overkill, but you may want to prepare your resources for Export using the technique described here: alexfeinberg.wordpress.com/2015/08/16/…. The main advantage of doing this is to prevent problems with multiple versions of the resource assembly getting loaded into same appdomain.
  • xr280xr
    xr280xr about 11 years
    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.
  • EngineerSpock
    EngineerSpock over 10 years
    What if YourAssembly is not contained inside the application path?
  • aroon65
    aroon65 over 10 years
    @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.
  • EngineerSpock
    EngineerSpock over 10 years
    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\
  • aroon65
    aroon65 over 10 years
    @EngineerSpock: this is a separate question, so please open one.
  • EngineerSpock
    EngineerSpock over 10 years
  • Austin Mullins
    Austin Mullins almost 10 years
    A more specific link to the exact section describing this syntax: msdn.microsoft.com/en-us/library/…
  • Andrejs Gasilovs
    Andrejs Gasilovs almost 7 years
    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.
  • user1040323
    user1040323 over 5 years
    This answer makes no sense. In order to follow it one already needs to know how to do it!