Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing a ResourceDictionary in another assembly

Tags:

c#

wpf

xaml

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.

like image 827
Pinx0 Avatar asked Jan 21 '26 16:01

Pinx0


1 Answers

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.

like image 184
DeshDeep Singh Avatar answered Jan 23 '26 04:01

DeshDeep Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!