Pinx0
Pinx0

Reputation: 1258

Referencing a ResourceDictionary in another assembly

I have a class library project that is common to several other projects. It's assembly name is "MyCompany" and its default namespace also "".

Then in one of these other projects (namespace "MyCompany.Something") I have the dll referenced, and I want to use a resource dictionary that I have in "MyCompany".

I found this: Add ResourceDictionary to class library

So I did exactly what it says, my xaml file located at "MyCompany" root directory is named "Recursos.xaml", built action set to Resource, not copy and blank custom tool and custom tool namespace, with the following content:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
    <ResourceDictionary Source="pack://application:,,,/Resources/Icons.xaml"/>
 </ResourceDictionary.MergedDictionaries>
  -- some other styles...
 </ResourceDictionary>

Then, in my WPF app project, I have in App.xaml the following:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MyCompany;component/Recursos.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Just added there the Controls.xaml for testing purposes. That one works, but mine doesn't:

An error occurred while finding the resource dictionary "pack://application:,,,/MyCompany;component/Recursos.xaml"

So I don't know why it doesn't recognize it. The reference is working as I can use every class from it.

Upvotes: 3

Views: 9164

Answers (3)

user5147454
user5147454

Reputation:

You need to add your ResourceDictionary as:

<ResourceDictionary Source="/MyCompany;component/Recursos.xaml" />

Upvotes: 3

dba
dba

Reputation: 1195

I assume this won't help you two years later. May do for someone else :) Make sure, to close VS and reopen the sln after referencing the external xaml... Did the trick for me... The XAML-Editor part is still not 100% (much better in VS2017 as it has been, but still... there are some issues like that)

AND don't forget to add:

System.Reflection.Assembly.LoadFrom("yourResourceAssemblyfullpath")

in the ctor of the Window/Control consuming the Resource BEFORE calling "InitializeComponent()"

    Sub New()

    System.Reflection.Assembly.LoadFrom("yourResourceAssemblyfullpath")
    InitializeComponent()

    End Sub

Otherwise you get a runtime error...

Upvotes: 0

DeshDeep Singh
DeshDeep Singh

Reputation: 1853

Try like this it will work: Project ABC has a reference to Project XYZ.

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

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

Upvotes: 4

Related Questions