270

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
  • 46,289
  • 20
  • 116
  • 131
Gus Cavalcanti
  • 10,527
  • 23
  • 71
  • 104
  • 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

7 Answers7

384

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

<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Subfolder/YourResourceFile.xaml"/>
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • 2
    What if YourAssembly is not contained inside the application path? – EngineerSpock 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\ – EngineerSpock 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 – EngineerSpock 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
  • 6
    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
106

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
  • 21,119
  • 15
  • 74
  • 98
Hertzel Guinness
  • 5,912
  • 3
  • 38
  • 43
  • 4
    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
  • 1
    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
40

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,792
  • 29
  • 44
23

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
  • 46,289
  • 20
  • 116
  • 131
10

For UWP:

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

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
  • 8,551
  • 6
  • 41
  • 66
4

I know I will probably go to WPF hell but I like to keep it simple.

In my "external" WPF project named MyCorp.Wpf.Dll where I have a folder called assets with my resource dictionaries

MyCorp.Wpf.Dll
|- Assets
   |- TextStyles.xaml
   |- Colours.axml

Let's assume I have this TextStyles.xaml with the UI font styles that I need to apply because I need windows 10/ 11 style compliance

    <Style x:Key="Header" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="Sego UI Light"/>
        <Setter Property="FontSize" Value="46" />
    </Style>
    
    <Style x:Key="Subheader" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="Sego UI Light"/>
        <Setter Property="FontSize" Value="32" />
    </Style>
    <Style x:Key="Title" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="Sego UI SemiLight"/>
        <Setter Property="FontSize" Value="24" />
    </Style>
    <Style x:Key="SubTitle" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="Sego UI Normal"/>
        <Setter Property="FontSize" Value="20" />
    </Style>
    
    <Style x:Key="Base" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="Sego Semibold"/>
        <Setter Property="FontSize" Value="15" />
    </Style>
    <Style x:Key="Body" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="Sego Normal"/>
        <Setter Property="FontSize" Value="15" />
    </Style>
    <Style x:Key="Caption" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="Sego Normal"/>
        <Setter Property="FontSize" Value="12" />
    </Style>   

</ResourceDictionary>

These styles are in my corporate style guide and I am re-sing them al over the place

now in my brand new application I can use the corporate style DLL from an internal NuGet package feed or I link it because It happens to be in my solution using the following resource dictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/MyCorp.Wpf;component/Assets/TextStyles.xaml"/>
        <ResourceDictionary Source="/MyCorp.Wpf;component/Assets/Styles.xaml"/>
        <ResourceDictionary Source="/MyCorp.Wpf;component/Assets/Brushes.xaml"/>
        <ResourceDictionary Source="/MyCorp.Wpf;component/Assets/ColorStyles.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

I have no clue where the extra component come from, I just know I needed. then in my new application I just link it like so:

<Application x:Class="MyNew.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ExternalResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            
            <BooleanToVisibilityConverter x:Key="VisibilityConverter"/>
        </ResourceDictionary>
    </Application.Resources>
</Application>

This way I have all external links in the ExternalResources.xaml where everyone understands where they came from and updating them is easy

I can then use the external resource definitions like any other in my windows, page and controls

<syncfusion:ChromelessWindow x:Class="IDPS.ChromelessWindow1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:IDPS"
        xmlns:r="clr-namespace:IDPS.Wpf.Properties;assembly=IDPS.Wpf"                     
        xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
        syncfusion:SfSkinManager.Theme="{syncfusion:SkinManagerExtension ThemeName=FluentDark}"
        mc:Ignorable="d"
       MinHeight="450" MinWidth="800">
    <Grid>
        <TextBlock Text="Hello world" Style="{StaticResource Title}"/>
    </Grid>
</syncfusion:ChromelessWindow>

My result

Walter Verhoeven
  • 3,867
  • 27
  • 36