ResourceDictionary in application resources cannot be found - wpf

I'm facing some strange issue with a ResourceDictionary in my Application.Resources.
In order to fill multiple ComboBoxes inside my application, I want to store the data in a ResourceDictionary.
However, I get the "Cannot find a Resource with that Name/Key..." error constantly.
My XAML-Code here:
<Application.Resources>
<ResourceDictionary x:Key="RDArray">
<sys:String x:Key="item1">Item1</sys:String>
<sys:String x:Key="item2">Item2</sys:String>
<sys:String x:Key="item3">Item3</sys:String>
</ResourceDictionary>
</Application.Resources>
<ListBox x:Name="lb" ItemsSource="{Binding Values, Source={StaticResource RDArray}}" />
Due to some lucky circumstances I was able to find out that putting another resource like Style above the Dictionary solves the problem.
<Application.Resources>
<Style x:Key="fubar" />
<ResourceDictionary x:Key="RDArray">
<sys:String x:Key="item1">Item1</sys:String>
<sys:String x:Key="item2">Item2</sys:String>
<sys:String x:Key="item3">Item3</sys:String>
</ResourceDictionary>
</Application.Resources>
The "bug" occurs in a WPF application as well as in Silverlight.
Although I can solve this using the shown "trick", I am curious where this error is coming from. I wasn't able to find anything about this. Maybe it is just me and something I am understanding wrong about resources in WPF.

This is because if you have a resource dictionary as the only item in the resources section then the contents simply get added to the parent dictionary (I snooped and this seems to be the case). To get around this you need to put your resource dictionary in separate xaml file (List.xaml in this case):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary x:Key="RDArray">
<sys:String x:Key="item1">Item1</sys:String>
<sys:String x:Key="item2">Item2</sys:String>
<sys:String x:Key="item3">Item3</sys:String>
</ResourceDictionary>
</ResourceDictionary>
and then reference that in your main app:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="List.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<ListBox x:Name="lb" ItemsSource="{Binding Values, Source={StaticResource RDArray}}"/>

Thanks for your answer.
In my real application I created that separated file, but I didn't make a second ResourceDictionary inside of it a nd just put the string values there. So it looked like:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<sys:String x:Key="item1">Item1</sys:String>
<sys:String x:Key="item2">Item2</sys:String>
<sys:String x:Key="item3">Item3</sys:String>
</ResourceDictionary>
So this was of course not working either.
Thanks for your answer, definitely solves this one.

Related

Cannot instantiate UserControl from another assembly - Resource cannot be found

I have a solution with two WPF projects: Primary and Secondary.
In the Primary project, a Window named PrimaryView instantiates an UserControl called SecondaryControl, defined in Secondary project.
SecondaryControl uses SecondaryStyle, which is defined in SecondaryResourceDictionary (as you might have guessed already, defined in SecondaryProject).
The fact is: when I try to run the solution, I get a XamlParseError, and digging InnerExceptions I eventually find the culprit, the ResourceNotFound error.
So my questions are:
If SecondaryControl and its SecondaryStyle are defined in the same assembly, why can't I instantiate it it PrimaryAssembly?
Should I make SecondaryStyle available to PrimaryProject namespace somehow? Why?
I try to help you by explanation how it works in my projects.
A separate assembly contains a common control and common resource dictionary like this:
CommonResources.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Some resources -->
</ResourceDictionary>
SomeCommonControl.xaml
<UserControl x:Class="YourAssembly.SomeCommonControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/CommonResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<!-- Some specific content -->
</UserControl>
I can use this control and resources from another assemblies and WPF-projects like this:
<Window x:Class="YourWPFProject.SomeWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="clr-namespace:YourAssembly">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/CommonResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<common:SomeCommonControl />
</Window>
I hope this will help you.

Referencing Resource Dictionaries in a separate project

I've recently split my silverlight application into several smaller projects.
I've moved all of the resource dictionaries containing my styles into a separate project ("Application.Themes") I then reference these from my App.xaml file within my main project.
This works fine for the main project however all other projects that reference styles within these resource dictionaries throw "Object Reference not set to an instance of an object" exceptions within the designer, although they do compile and run without any problems and with the correct styles.
I've added an App.xaml file to each of the individual projects which references the same dictionaries as my main App.xaml file, this has made no difference.
Is there a correct way to reference resource dictionaries from another project which allows the designer to be used?
EDIT:
Here is some more information and some code snippets to demonstrate the issue I'm having
I have a styles project called "Themes" within this project I have several dictionaries that define all of the styles for the project.
Within my main App.xaml I have the following
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes;component/Styles/CoreStyles.xaml"/>
<ResourceDictionary Source="/Themes;component/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
If I reference styles within the main project they work correctly. however they don't for any other projects even if those projects reference the Themes project.
I've attempted to put the following at the start of each UserControl in order to resolve the styles at design time, however it still cannot resolve styles that are within the project.
<UserControl>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes;component/Styles/CoreStyles.xaml"/>
<ResourceDictionary Source="/Themes;component/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Additional Control Specific resources -->
</ResourceDictionary>
</UserControl.Resources>
<!-- The following resources are defined in Styles.XAML and don't resolve at design time and throw errors -->
<TextBlock Text="Header Test"
FontFamily="{StaticResource HeaderFontFamily}"
Foreground="{StaticResource StrongBrush}">
</UserControl>
My styles.xaml looks similar to this.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:behaviors="clr-namespace:Minerva.Presentation.Behavior;assembly=Minerva.Presentation"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
<SolidColorBrush x:Key="StrongBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.StrongColor}" />
<FontFamily x:Key="HeaderFontFamily">Segoe UI Light, Lucida Sans Unicode, Verdana</FontFamily>
</ResourceDictionary>
Create an assembly for the styles/themes project so that this can be referenced by other projects.
In order to merge these styles into application either at app.xaml/page.xaml using MergedDictionaries
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Assembly;component/Stylesorthemes.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Hope this is useful.
I created an assembly test which contains the resource dictionary Theme.xaml
Theme.xaml code
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Thickness x:Key="GeneralThickness">10</Thickness>
</ResourceDictionary>
I created seperate silverlight project testreturns
case1.In App.xaml
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="testreturns.App"
>
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/test;component/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
case2.Usercontrol level
<UserControl.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/test;component/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
And using it to set borderthickness of button
<Button Height="50" Width="150" BorderThickness="{StaticResource GeneralThickness}"/>
In both cases it is working for me.
Is this what you intend for?
Did you set the theme.xaml file properties BuildAction to Resource while creating assembly?

Accessing resources during initialization of control

lets assume we have the following control definition
<ctrl:ChildWindow x:Class="Control.Editor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:local="clr-namespace:Control"
Width="400" Height="300"
local:AttachedProperties.DialogResult="{Binding Path=DialogResult}"
Title="{Binding Path=Caption}" Style="{StaticResource Title}" DataContext="{Binding}" HasCloseButton="False">
<ctrl:ChildWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Control;component/Resources/BaseAppearance.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ctrl:ChildWindow.Resources>
</ctrl:ChildWindow&gt
The problem is that the style on the root control cannot be set because the ResourceDictionary is not loaded.
How can I get access to the StaticResource Title during the initialization of the control, when I don't have access to the App class? I'm also not sure that it would be possible, if I would have access to it.
Regards
I'd recommend accessing your resource and doing the work in the .Loaded() event of your control.
Edit: On second thought... I think I know what you're doing now... You have a resource set in your App.xaml class, but you want to access it in your control.
Easy way around the problem is to set it to a DynamicResource instead... but this is less performant.
What is the BuildAction set to on your App.xaml in the property's tab?
If it is ApplicationDefinition... then you should be able to access your resource as you currently are.
I found the common way without using code behind. I knew it is possible. ^^
<ctrl:ChildWindow x:Class="Control.Editor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:local="clr-namespace:Control"
Width="400" Height="300"
local:AttachedProperties.DialogResult="{Binding Path=DialogResult}"
Title="{Binding Path=Caption}" DataContext="{Binding}" HasCloseButton="False">
<ctrl:ChildWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Control;component/Resources/BaseAppearance.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ctrl:ChildWindow.Resources>
<crtl:ChildWindow.Style>
<StaticResource ResourceKey="Title" />
</crtl:ChildWindow.Style>
</ctrl:ChildWindow&gt

Why can't I find DataTemplates in merged resource dictionaries?

In my MainWindow.xaml, I have the following reference to a ResourceDictionary:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
In MainSkin.xaml, I define a datatemplate:
<DataTemplate x:Key="TagTemplate">
...
</DataTemplate>
Deeper within my application, I attempt to use this data template:
<ContentControl DataContext="{Binding Tag}" ContentTemplate="{StaticResource TagTemplate}"/>
The code compiles successfully, but when I attempt to load a Page or UserControl that contains this StaticResource, I get an exception saying that the TagTemplate can't be found.
What am I doing wrong?
In order to access the contents of a resource defined in a XAML file, you need to "include" that XAML file in each page and control that uses it. So every XAML files will need to have the MergedDictionaries entry that you have in MainWindow.xaml.
Alternatively you can add those merge dictionaries to App.xaml and those resources are included implicitly:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Are you using that StaticResource in the same Window where it is declared? Otherwise I think that you cannot have access to that.

How to add more than one resource to a XAML window?

I have a little problem right now and I don't know how to fix it. I want to add two resources to a window. One is a XAML File style resource, the other a ValueConverter Class.
Both of them work if I use only one resource at a time:
<Window.Resources>
<ResourceDictionary Source="Resources\MyStyles.xaml" />
<Window.Resources>
or
<Window.Resources>
<local:MarginConverter x:Key="adjustMargin"/>
</Window.Resources>
But if I try something like this:
<Window.Resources>
<local:MarginConverter x:Key="adjustMargin"/>
<ResourceDictionary Source="Resources\MyStyles.xaml" />
</Window.Resources>
I get the message the resource is already been set and can not set twice.
I have no idea how to get this done. Is there something like a resource group?
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\MyStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<local:MarginConverter x:Key="adjustMargin"/>
</ResourceDictionary>
</Window.Resources>

Resources