I have defined a Reource Dictionary in a WPF Class Library containing colors and brushes, called BrushResources.xaml.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Lots of Colors and Brushes here>
</ResourceDictionary>
I want to use some of the Brushes in code from another assembly, which references this library project. How do get an Instance of ResourceDictionary
of it?
You dont have to put your ResourceDictionary inside a "App.xaml". If you have a UserControl or Window you could link to your ResourceDictionary.
If you know the other
assembly
structure then use below code: XAML SolutionOutput: If we want to use
ResourceDictionary
RD1.xaml
of ProjectWpfControlLibrary1
intoStackOverflowApp
project.Structure of Projects:
Resource Dictionary
Code Output:
c# Solution:
use this link.
What you're asking is a component of the functionality necessary to provide true skinning in an application. Getting resources from a separate assembly involves reading the compiled XAML, or BAML from the other assembly. Here is a method I use in a skinning library to retrieve the BAML from an assembly:
Then, to convert the BAML to specific resources, you do the following:
And in order to merge the resources from the other assembly into the current assembly:
This example does the work in a very generic manner for skinning purposes, but you can streamline this to accomplish your specific goal if necessary. You can see a skinning library that uses this method here on github, with a few examples to demonstrate.
why not simply use MergedDictionaries in your other assembly?