XAML ResourceDictionary same-assembly reference - wpf

I'm developing a WPF control library, and I need to reference a resource dictionary defined in the same assembly.
I managed to get it working with a separate-assembly reference.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/MyLocalAssembly;component/Foo.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
While with a same-assembly reference it doesn't work, and raises an exception ("Cannot locate resource 'Foo.xaml'.") at load time. Note that in the designer everything works fine, no matter which method I use.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/Foo.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

Don't use absolute path for the resource dictionary. Simply use relative path reference.
For example,
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="folder/Foo.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

Related

<BitmapImage/> in <ResourceDictionary.MergedDictionaries/ResourceDictionary> fails to load

I'm scratching my head as to why the following two result in very different behaviour:
<Application x:Class="MN.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="Theme.xaml" />
<ResourceDictionary>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<BitmapImage x:Key="PlaceholderImage" UriSource="/MN;component/Lib/PlaceholderImage.png"/>
</ResourceDictionary>
</Application.Resources>
</Application>
The above works in that it loads my Theme.xaml ResourceDictionary, and I seem to be able to access the PlaceholderImage BitmapImage with no issue.
However:
<Application x:Class="MN.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="Theme.xaml" />
<ResourceDictionary>
<BitmapImage x:Key="PlaceholderImage" UriSource="/MN;component/Lib/PlaceholderImage.png"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
In this snippet I moved the BitmapImage into the inline ResourceDictionary within MergedDictionaries. Now when I try to run the application I get the following exception:
System.Windows.Markup.XamlParseException: ''Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception.' Line number '11' and line position '44'.'
Looking up the exception it seems like this is generally thrown when the image file could not be resolved, but I'm not sure why that would happen, and I feel like there is some key mechanism that I'm failing to understand here.
I'm looking for insights as to why it doesn't work, how one might typically add a BitmapImage to a merged dictionary.

How to include and use Themes in wpf application

I have to include themes to my wpf application and use them for look and feel of the windows(.xaml) and controls.
i tried to include the theme files like below code but it is throwing error.
<Application.Resources>
<ResourceDictionary Source="\Themes\BureauBlack.xaml"/>
How to assign source path of a theme file which is assign Build action as Resource
Try to use MergedDictionaries:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Generic_UI/BlueColors.xaml"/>
<ResourceDictionary Source="Generic_UI/KStyles.xaml"/>
<ResourceDictionary Source="Generic_UI/KButtons.xaml"/>
<ResourceDictionary Source="Generic_UI/KWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

Import multiple resource files in app.xaml wpf

[Running on Windows 8]
I know how to import a single one, like this:
<Application.Resources>
<ResourceDictionary Source="Styles\MyCombobox.xaml"/>
</Application.Resources>
but I want something like this:
<Application.Resources>
<ResourceDictionary Source="Styles\X.xaml"/>
<ResourceDictionary Source="Styles\Y.xaml"/>
<ResourceDictionary Source="Styles\Z.xaml"/>
</Application.Resources>
I get:
The property Resource can only be set once!
You should merge that dictionaries.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles\X.xaml"/>
<ResourceDictionary Source="Styles\Y.xaml"/>
<ResourceDictionary Source="Styles\Z.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
More in documentation
UPDATE
Of course, you should wrap property usage in ResourceDictionary tag.

WPF/XAML Error using ResourceDictionary in App.xaml

I'm having trouble referencing a xaml resource dictionary file, leading to a runtime error saying 'resource x can't be found' when referring to a ResourceDictionary inside of . But using the Window.Resources node instead works.
working code:
<Window.Resources>
<ResourceDictionary Source="Resources\Template_1.xaml">
</ResourceDictionary>
</Window.Resources>
my malfunctioning code:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\Template_1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
My problem is I need the dictionary in application scope.
I'm accessing it as StaticResource and the build action of Template_1 is set as 'Page'
Any suggestions?
Edit:
this is the structure of Template_1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:self="clr-namespace:Application_1"
>
<ControlTemplate x:Key="dict_TabContent" x:Shared="true" >
<DockPanel>
...
</DockPanel>
</ControlTemplate>
</ResourceDictionary>

Merged dictionaries and local resources

I have a Styles.xaml that groups many ResourceDictionarys inside a MergedDictionary.
I imported Styles.xaml in my UserControl.Resources
<UserControl.Resources>
<ResourceDictionary Source="Dictionaries\Styles.xaml" />
</UserControl.Resources>
but when I try to add a converter
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionaries\Styles.xaml" /> <--! Exception -->
</ResourceDictionary.MergedDictionaries>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</ResourceDictionary>
</UserControl.Resources>
it raises
ArgumentNullException: Value cannot be null.
Parameter name: item
Wrapping the converter inside another MergedDictionary has no effect.
How can I solve this?
Thank you all!
SOLVED
I eventually figured it out: the Exception was raised inside one the .xaml files, but Visual Studio does not provide enough info to locate the faulty line.
Following code does work.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionaries\Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</ResourceDictionary>
</UserControl.Resources>
Try this
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/PROJECTNAMESPACE (TestProject.Something);component/Dictionaries/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</ResourceDictionary>
</UserControl.Resources>

Resources