3

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.

Pinx0
  • 1,248
  • 17
  • 28

3 Answers3

4

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.

DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43
3

You need to add your ResourceDictionary as:

<ResourceDictionary Source="/MyCompany;component/Recursos.xaml" />
Pinx0
  • 1,248
  • 17
  • 28
  • So, as I can understand, you've added this library as a external reference? It's not another project in your solution? –  Jul 30 '15 at 07:02
  • No, it's a different solution, it's been compiled and then added as reference by examining and selecting the dll. – Pinx0 Jul 30 '15 at 07:19
0

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

dba
  • 1,159
  • 1
  • 14
  • 22