WPF: Application resources equivalent in DLL

c#-4.0resourcedictionarywpfwpf-controls

I have two projects in my solution. The first project is a WPF application, the other is a regular DLL project. Inside the DLL project I have some WPF user controls. I want these controls to share some resources and define them in the DLL.

I know that in a regular WPF application, you can specify application resources in App.xaml. Is there an equivalent in a DLL project?

Best Answer

Yes, you can create a resource XAML in the DLL like this (keep sure you have all WPF assemblies referenced in the DLL):

<!-- name of the dictionary is MyResources in MyDLL namespace -->
<ResourceDictionary x:Class="MyDLL.MyResources"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="./Controls/ButtonStyles.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

and add this to the resources of your App class in your WPF project:

public App()
{
    MyDLL.MyResources externalRes = new MyDLL.MyResources();

    this.Resources.Add("MyExternalResources", externalRes);
}