Multiple XAML resource files in one assembly, referenced in another assembly - wpf

Good evening. A WPF XAML resource question please.
I have one user-control project called Resources. In this project I have a directory that contains multiple XAML files. I have merged these resources in a Main.xaml file in the project root directory.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources;component/xaml/Buttons.xaml" />
<ResourceDictionary Source="Resources;component/xaml/Images.xaml" />
<ResourceDictionary Source="Resources;component/xaml/Styles.xaml" />
<ResourceDictionary Source="Resources;component/xaml/Tooltips.xaml" />
</ResourceDictionary.MergedDictionaries>
The Resource project compiles without issue and there are no visual errors in the code you see above.
I have a second project called Buttons. In the App.xaml, I reference this Resource project.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources;component/Main.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I have referenced Resources.dll in the Button project. Visual Studio displays an error line under the ResourceDictionary statement; howbeit the project compiles.
When I run Button.exe, I get the error "Cannot locate resource 'resources;component/xaml/buttons.xaml'."
My ResourceDictionary statement matches what I have successfully done in other projects. Assuming what I have written in App.xaml in the Button project is correct, what should I do differently in the Resources' Main.xaml file?
Thanks in advance.

Okay, I figured it out. I'll document how to do this in case anyone else runs across this.
In my one assembly entitled Resources, I have a Main.xaml file in the project root. I have a subdirectory entitled "xaml" where I have several xaml files. My mistake was in the leading backslash on the directory "xaml".
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="xaml/Buttons.xaml" />
<ResourceDictionary Source="xaml/Images.xaml" />
<ResourceDictionary Source="xaml/Styles.xaml" />
<ResourceDictionary Source="xaml/Tooltips.xaml" />
</ResourceDictionary.MergedDictionaries>
Then, in another project called Buttons, in the App.xaml file, I reference that Main.xaml file in the MergedDictionaries section.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources;component/Main.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Once I corrected the references in the main file, everything works correctly.
So this allows me to split up my resources into separate xaml files and reference them in other assemblies. Wohoo!

Also, for completeness, for each of the xaml files (including main.xaml), set the Build Action to Page (right-mouse click on the xaml file and select Properties).

Related

WPF Using multiple Resource Dictionaries from multiple projects

I have two class Library projects:
Project A.Themes
Project B.Themes
Project A.Themes is my base Themes Project.
Project B.Themes using A.Themes and have new styles and some of the resources have keys that already defined in A.Themes.
We want to use this two themes in our Project, and if we use a resource that is defined in both of the project we want to take the resource from B.Themes.
This is our code:
A.Themes have few files of styles:
Brushes.xaml
Buttons.xaml
CheckBox.xaml
etc..
we load them in Bundle.Xaml:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/A.Themes;component/Assets/Brushes.xaml"/>
<ResourceDictionary Source="pack://application:,,,/A.Themes;component/Assets/Buttons.xaml"/>
<ResourceDictionary Source="pack://application:,,,/A.Themes;component/Assets/CheckBox.xaml" />
</ResourceDictionary.MergedDictionaries>
B.Themes have the same Files:
Brushes.xaml
Buttons.xaml
CheckBox.xaml
we load them in Bundle.Xaml and adding the bundle of A.Themes:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/A.Themes;component/Bundle.xaml"/>
<ResourceDictionary Source="pack://application:,,,/B.Themes;component/Assets/Brushes.xaml"/>
<ResourceDictionary Source="pack://application:,,,/B.Themes;component/Assets/Buttons.xaml"/>
<ResourceDictionary Source="pack://application:,,,/B.Themes;component/Assets/CheckBox.xaml" />
</ResourceDictionary.MergedDictionaries>
In our Project we load them in App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/A.Themes;component/Bundle.xaml"/>
<ResourceDictionary Source="pack://application:,,,/B.Themes;component/Bundle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
the problems are:
1. it not always takes the resources from B.Themes and we can't find out why.
2. if i remove the reference to A.Themes/Bundle.xaml from app.xaml the project can't find resources from A.Themes even though it's included in B.Themes/Bundle.xaml
note:
we have refernce to A.Themes project in B.Themes
and refernce to A.Themes and B.Themes in main project
can someone please help me to understande what is going on here?
thanks!
The loading order isn't quite what you expect. From MSDN:
Resources in a merged dictionary occupy a location in the resource lookup scope that is just after the scope of the main resource dictionary they are merged into
https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/merged-resource-dictionaries
So the dictionaries that merge into Bundle.xaml of assembly A are actually loaded after the other ones.
Please refer to the following link for more information and an example of the same behaviour: https://social.msdn.microsoft.com/Forums/vstudio/en-US/3bea80f9-d1db-4cb7-ae7a-77a02eaf4ec9/resourcedictionary-load-order?forum=wpf

How to reference an assembly (residing in relative path) using URI pack syntax?

I have this kind of the solution folders structure:
C:\TfsBindRoot\MySln\Development\Sources\WpfProj
C:\TfsBindRoot\MySln\Development\Sources\Resources
Resources project is referenced in WpfProj.
I tried different variants such as
<ResourceDictionary Source="pack://application:,,,//Resources" />
//and
<ResourceDictionary Source="pack://application:,,,/../Resources" />
//and
<ResourceDictionary Source="pack://application:,,, ../Resources" />
But it does not see the library.
It appears that the path you are providing in not in correct format.
The path depends it the resources are in the same package or different package. Since your question does not provide details of your application pack, check MSDN site for details for the proper format.
UPDATE
Resource file in subfolder of referenced assembly ->
"pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml"
Resource file in versioned referenced assembly ->
"pack://application:,,,/ReferencedAssembly;v1.0.0.0;component/ResourceFile.xaml"
UPDATE 2
I tried and it worked. "ResourceLibrary" was the assembly name.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ResourceLibrary;component/Resources/ResourceFile.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
One thing you might be missing is the ResourceFile.xaml where your might have kept all your resources. My ResourceFile.xaml looks like:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="MyBrush" Color="Cornsilk"/>
</ResourceDictionary>
<ResourceDictionary Source="pack://application:,,,/Resources;component/YourResources.xaml" />
YourResources.xaml replace with name of xaml file which contains resources.

Unable to merge ResourceDictionary from a referenced project

I have two assemblies that each provide a common set of styles and resources that I want to include in my application. I'm using merged dictionaries in my App.xaml in order to load them, and at runtime they were just fine. Unfortunately, these resources won't load at design time, filling my Errors window with messages about unresolvable resources and giving me a UI that doesn't represent what will actually appear.
This is my App.xaml as it stands right now:
<Application x:Class="ClientDebug.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Dai.Common;component/Xaml/Common.xaml" />
<ResourceDictionary Source="/Dai.DevExpress;component/Xaml/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
For both of these merged dictionaries, I get the following errors in my Errors window:
Error 11 An error occurred while finding the resource dictionary "/Dai.Common;component/Xaml/Common.xaml". C:\DevProjects\Core Application\ClientDebug\App.xaml 12 17 ClientDebug
Error 12 An error occurred while finding the resource dictionary "/Dai.DevExpress;component/Xaml/Styles.xaml". C:\DevProjects\Core Application\ClientDebug\App.xaml 13 17 ClientDebug
Which are obviously short on helpful information. Again, they load at runtime just as you'd expect, but none of the resources are available at design time.
I have this every time I start a new WPF project or work with an old one ever since a few days ago. I started a question to ask for help on it, only for it to disappear on me on a test Solution! Seems Visual Studio's not very stable, who knew?
The core of the problem is that the Xaml designer isn't allowing you to make references to other Projects in your Solution from your Application Project only. The workaround is to move everything into a Library and have the Application do little more than hand off control to it in code. Inelegant and the loss of App.Xaml is very tough, but it's much better than waiting for Visual Studio to magically figure it all out.
I precede the dictionaries that are from other namespaces with "pack://application:,,," like so:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Wsi.Util;component/DataGrid/NameValueDataGridDictionary.xaml" />
<ResourceDictionary Source="LayoutStylesDictionary.xaml" />
<ResourceDictionary Source="pack://application:,,,/Wsi.Util;component/controls/ToolBarResources.xaml" />
<ResourceDictionary Source="pack://application:,,,/Wsi.Common;component/view/themes/FilterStylesResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
HTH,
Janene
I tried a similar merging in a project of mine and I seem to have it working perfectly. (or so, forgive me if its some settings of mine that I may have set long ago..) The only question I can ask is if the xamls are referring to anything within the parent project? I am assuming you have a reference to the parent project also (just cross-checking). Maybe the xaml file needs another xaml file too (controls, colors, fonts etc).
(The merging that I tried was from MahApps.)
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I fixed this issue by signing the referenced project.
Problem was: My project was signed with a key file but the referenced project not.
To solve:
Load the project into the solution
Opened the project properties
Navigate to 'Signing'
Click 'Sign the assembly'
Select '<New...>' from the combo box
Enter a name, eg. 'keyfile' ('Protect my keyfile with a password' is optional)
Rebuild solution

Global style in a separate assembly

For my application themes, I created a separate class library MyApp.Themes.dll.
In the root folder of this library I have Standard.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/MyApp.Themes;component/Standard/Accordion.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
In Standard folder I have Accordion.xaml:
<ResourceDictionary xmlns:layoutPrimitivesToolkit="..."
xmlns:layoutToolkit="..."
...>
<!-- layoutPrimitivesToolkit:AccordionButton -->
<Style TargetType="layoutPrimitivesToolkit:AccordionButton">
...
</Style>
...
</ResourceDictionary>
which are default styles for WPF Toolkit Accordion control, except that style for
AccordionItem is modified to set background to transparent (instead of blue, which is a known bug).
For both xaml files Build Action is set to "Resource".
Now, after referencing MyApp.Themes library in MyApp WPF project, in App.xaml I wrote the following:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/MyApp.Themes;component/Standard.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
It doesn't work (no error; just style not applied). If I copy&paste Accordion.xaml styles directly in App.xaml, then it works.
Some people suggested adding dummy resource dictionary, so I added the following in App.xaml after MergedDictionaries ending tag:
<Style TargetType="{x:Type layoutToolkit:AccordionItem}" />
Again, it doesn't work. Can someone help me solve this? By the way, I didn't try an approach
suggested by some others - using basedOn. If I must do that, I'd like to do in MyApp.Themes,
because it makes using themes nicer in App.xaml. Thank you in advance.
UPDATE1: It works if I directly copy&paste Accordion.xaml content to Standard.xaml
UPDATE2: I tried local assembly resource file URI (http://msdn.microsoft.com/en-us/library/aa970069.aspx) in Standard.xaml:
<ResourceDictionary Source="pack://application:,,,/Standard/Accordion.xaml"/>
It throws an error, saying standard/accordion.xaml not found.
It seems there's a bug in .NET. See the thread below:
Trouble referencing a Resource Dictionary that contains a Merged Dictionary
Also, there's a connect page on Microsoft site:
https://connect.microsoft.com/VisualStudio/feedback/details/609601/merge-dictionaries-does-not-work-when-we-merge-merged-dictionaries#tabs

Trouble referencing a Resource Dictionary that contains a Merged Dictionary

I have a library, CommonLibraryWpfThemes, with several Resource Dictionary XAML files in it. My Themes/Generic.xml file contains a ResourceDictionary.MergedDictionaries declaration that merges all the other files together.
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
In my application project, I have a reference to CommonLibraryWpfThemes, and I explicitly reference Generic.xml in my App.xaml file.
App.xaml -- FAILS
<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />
</Application.Resources>
</Application>
This doesn't work. I get the following error when I run my app:
System.Windows.Markup.XamlParseException occurred
Message="Cannot find resource named '{_fadedOrangeBrush}'. Resource names are case sensitive. Error at object 'System.Windows.Setter' in markup file 'CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml' Line 18 Position 13."
Source="PresentationFramework"
LineNumber=18
LinePosition=13
If I place the contents of Generic.xaml into App.xaml directly, everything works fine:
App.xaml -- SUCCEEDS
<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Maybe I'm going about this in the wrong way. My goal is to make it easy to reference all my theme resources from multiple applications without having to list out all the individual files. Is there a recommended way to do this? (Note: I'm not trying to switch between multiple themes--I just have one theme.)
As a bonus, it would be nice if someone could tell me how to reference resources in an external library without breaking the designer in Visual Studio.
Thanks.
EDIT:
I tried wrapping the ResourceDictionary in a ResourceDictionary.MergedDictionary element, but that also didn't work (I get the same error):
<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Answered a similar question here earlier, see Adding a Merged Dictionary to a Merged Dictionary question.
This is an optimization bug, see Microsoft Connect / DefaultStyleKey style not found in inner MergedDictionaries:
On the creation of every object in
XAML, if a default style is present
(i.e. style w/ a key of Type) that
style should be applied. As you can
imagine there are several performance
optimizations to make that (implied)
lookup a light weight as possible. One
of them is that we don’t look inside
Resource Dictionaries unless they are
flagged as “containing default
Styles”. There is a bug: if all your
default styles are nested in merged
dictionaries three levels deep (or
deeper) the top dictionary does not
get flagged so the search skips it.
The work around is to put a default
Style to something, anything, in the
root Dictionary.
So adding a dummy style to the root dictionary fixes this. Example
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Dummy Style, anything you won't use goes -->
<Style TargetType="{x:Type Rectangle}" />
</ResourceDictionary>
</Application.Resources>
</Application>
Check your constructor in App.xaml.cs calls InitializeComponent() - this is what merges the resource dictionaries...
You should not have to reference generic.xaml at all, it has built-in support. This however means that it provides default styling, which you do not set explicitly. Explicitly set styles/templates need to be attainable from explicitly referenced res dictionaries.
(EDIT for clarity)
One exception to this is the App.xaml, where defined resources become accessible by the whole app, without requiring to reference any specific resource dictionary. The resource itself, would have to be accessible by name.
The reason why this fails
<Application.Resources>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />
</Application.Resources>
is, I think, because you didn't wrap it in a MergedDictionary wrapper, adding it to merged dictionaries. Adding directly to resources works only for resources you declare locally, e.g. the styles, etc. themselves.
However, as I said before, you shouldn't have to merge generic.xaml anywhere, maybe you should just refactor brushes and other resources used outside styles, and merge only those resources in app.xaml.
Also note that styles do not have to be in generic.xaml to have "default style" behaviour - if a style with key equal to the type of the element is accessible to it (globally, or in local resources), then it will use the style as a default style. The generic.xaml is just a convenience.
Check this answer.
For other custom brushes, etc, you need to reference those resources explicitly.
You should also check the contents of the WindowDictionary.xaml, this error has a certain smell about it.
I was getting this error in my unit tests and Chris' answer from above gave me the clue I needed. Basically on my first tested method, I put:
MyApplication.App app = new MyApplication.App();
app.InitializeComponent();
And suddenly it could find my template for my pages. Note: this does mean that you have to check to see if an instance of your App already exists if you are unit testing your App.cs as well.
My solution is here, click Workarounds.

Resources