226

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?


  • 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. - user195275

6 답변


329

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

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


  • What if YourAssembly is not contained inside the application path? - EngineerSpock
  • @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
  • 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
  • @EngineerSpock: this is a separate question, so please open one. - Kent Boogaart
  • This answer makes no sense. In order to follow it one already needs to know how to do it! - user1040323

90

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>

Cheers


  • 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
  • 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. - Gattuso

33

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


16

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


5

For UWP:

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


4

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.

Linked


Related

Latest