Import multiple resource files in app.xaml wpf - 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.

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.

WPF Merged Dictionaries

i'm trying to use MaterialDesginThemes and HandyControl ResourceDictionary together but after i run my application it seems that it only recognising Handycontrol recourses at then i find out that application resources can only recognize the last line in the recourse dictionary can any one help how to fix this problem
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Purple.xaml" />
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

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>

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>

XAML ResourceDictionary same-assembly reference

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>

Resources