Reputation: 198
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?
Upvotes: 9
Views: 9626
Reputation: 24453
No, there isn't an equivalent in a dll, because the resource loading isn't part of an assembly (exe), but part of an application. In order to load resources an application must be loaded. App is the root element of an application, rather than an exe assembly. To do the equivalent for controls in a dll you can create a separate ResourceDictionary and add it to each control's XAML by merging it into the UserControl's resources using ResourceDictionary.MergedDictionaries.
Upvotes: 3
Reputation:
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);
}
Upvotes: 6