Link to home
Start Free TrialLog in
Avatar of jxbma
jxbmaFlag for United States of America

asked on

How do you reference a XAML DictionaryResource from an external Assembly?

Hi:

I'm a total Nubie to the world of WPF.
I'm just starting to get immersed in it.

I've got an issue I'm trying to figure out with using ResourceDictionary and Merged Dictionaries.

I've got WPF sample application that I've converted to a Class Library.
That newly converted assembly is included/referenced in a library of user controls that I'm trying to create.
That converted assembly is included a project in the solution containing my user control library (dll) project.

I was able to convert it successfully and got rid of all the errors except for some issues
with the ResourceDictionary.

This is what the offending XAML looks like:
-----------------------------------------------
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ConvertedAppPath/Dictionaries/Brushes.xaml" />
                <ResourceDictionary Source="/ConvertedAppPath/Dictionaries/Styles.xaml" />
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

The original WPF application had several Resource Dictionary items included in the assembly.
When I converted to a class library, I'm getting the following error message on resource dictionary:
=============================================================
Error      21      An error occurred while finding the resource dictionary "/ConvertedAppPath/Dictionaries/Brushes.xaml".      C:\MyExplicitPath\WPFUserControls\DynamicDataChart.xaml      15      17      WPFUserControls
Error      22      An error occurred while finding the resource dictionary "/ConvertedAppPath/Dictionaries/Styles.xaml".      C:\MyExplicitPath\WPFUserControls\DynamicDataChart.xaml      15      17      WPFUserControls


So, I thought that perhaps I needed a relative path for the ResourceDictionaries.
-----------------------------------------------------------------------------------------
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../ConvertedAppPath/Dictionaries/Brushes.xaml" />
                <ResourceDictionary Source="../ConvertedAppPath/Dictionaries/Styles.xaml" />
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

I get the following error message(s) inside the XAML editor:
-------------------------------------
The file "../ConvertedAppPath/Dictionaries/Brushes.xaml" is not part of the project or it's Build Action property is not set to "Resource".


I'd really like to understand this, so I've got a couple of questions:
--------------------------------------------------------------------------
1) How do I clean this up to get rid of the errors?

2) Do I need to explicitly set the assembly location in "Source"?

3) What are all the pieces of the "Source" property and what do they really mean?

4) Why did the 2nd error message not get displayed at compile time, but is visible only in the XAML editor?    


Thanks for your response and efforts,
JohnWPFNewB
mockup.png
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

It sounds like you are having a problem with the relative path names.  What is the build action for the resource .xaml files?
Avatar of jxbma

ASKER

The "Build Action" for the resource .xaml files is set to "Page."
This is the original default setting from the converted Class Library was originally a WPF app.
Did you try changing the build action to Resource?
Avatar of jxbma

ASKER

Yes, I did and I still get the same error.
That is the "or" part of the error description.  The other part is how the relative path gets resolved.  

How does this path get resolved?

     ../ConvertedAppPath/Dictionaries/Brushes.xaml
Avatar of jxbma

ASKER

../ConvertedAppPath/Dictionaries/Brushes.xaml

ConvertedAppPath --> Just happens to have the same name as my assembly.
So my assembly name is the same name as the VS2010 project.

So I tried:
<ResourceDictionary Source="/AssemblyName;../AssemblyName/Dictionaries/WindowsStyles.xaml" />

Can you give me an example of what this should really look like?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jxbma

ASKER

OK, except remember my original scenario.

I converting a "WPF App" into a class library.
I'm then referencing that class library/assembly from another class library/assembly.

Avatar of jxbma

ASKER

OK. This is still not working and it's driving me nuts.
I've simplified this to try to figure out how this should work.
I created a "Test" WPF Class Library called "TestForDictionary".
To it, I have added a single Resource Dictionary called "Dictionary1.xaml".

I want to refer to it from another WPF Class Library (which contains my user defined controls).

My User Control xaml contains the following:
-------------------------------------------------
 <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/TestForDictionary;/TestForDictionary/Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
</UserControl.Resources>

I clean, then rebuild all and get the following error message:
------------------------------------------------------------------
Error      1      An error occurred while finding the resource dictionary "pack://application:,,,/TestForDictionary;/TestForDictionary/Dictionary1.xaml".      C:\..\WPFUserControls\DynamicDataChart.xaml      15      17      WPFUserControls

I've tried setting the "Build Action" to both "Page" and "Resource" in the properties of Dictionary1.xaml.
I get the same error message both times.

What am I missing/doing wrong here?
This should be easy. It's like trying to deal with library/dll stuff in years past.

Please provide a simple/detailed/concise example (speleed out explicitly) that works for you.

Thanks,
JohnB
Avatar of jxbma

ASKER

OK. I wasn't able to get it quite to work, so I refactored code and pulled out the resource dictionairies into my current Class Library.