We help IT Professionals succeed at work.

How do I resolve a "Cannot locate resource" exception thrown when loading a WPF user control from a WPF Class Library?

jxbma asked

I'm a newbie at the world of WPF.

As a starting point/exercise, I've taken an existing WPF app (with subsequent customized controls) and
converted it to a class library. The library (dll) build without an warning/error messages.

I've trying to embed a control from the class library inside a window/control in my .Net winforms application.
I'm using the ElementHost control . The goal is to host the WPF control within the ElementHost control.
This piece compiles and looks fine.

However when I run my application,  I get the following exception:
"'Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '15' and line position '18'."      string
Systeim.IO.IOException -->  "Cannot locate resource 'dictionaries/brushes.xaml'."      string

The Class Library contains a couple of xaml resources that are shared amongst the WPF controls.
They are part of the Class Library's VS solution/project. I'm assuming that they get built into
the Class Library/DLL, otherwise my Class Library would not build.

I noticed that the "Exe" version had an "App.xaml" file which had the following section defined:
              <ResourceDictionary Source="ResourceDictionaries/Brushes.xaml" />
               . . .


What am I missing here?
Does the Class Library contain some sort of similar definition/construct?

Any help would be greatly appreciated, as I'm trying to throw together a quick demo and have already been working on this issue for several hours.

Thanks in advance,

Watch Question

jxbmaSoftware Consultant


Ultimately, after some refactoring, this solution worked.

Thanks for the advise!