So I have a Windows Universal Class Library that has a resource dictionary in it that I want to merge with my Windows 10 Universal Application's main resource dictionary in App.xaml.
My App.xaml simply merges in my main Resource dictionary from the same assembly.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Then from my main resource dictionary (Styles/Styles.xaml) I am merging in other resource dictionaries from the same assembly. This is where I would like to merge in a resource dictionary from another assembly:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Fields.xaml"/>
<ResourceDictionary Source="DataTemplates.xaml"/>
<!--<ResourceDictionary Source="/{AssemblyName};component/Shared.xaml" />-->
<!--<ResourceDictionary Source="pack://application:,,,/{AssemblyName};component/Shared.xaml" />-->
<ResourceDictionary Source="ms-appx:///{AssemblyName}/Shared.xaml" />
</ResourceDictionary.MergedDictionaries>
I've tried adding this to my main resource dictionary:
<ResourceDictionary Source="/{AssemblyName};component/Shared.xaml" />
and this...
<ResourceDictionary Source="ms-appx:///{AssemblyName}/Shared.xaml" />
Based on this article about Windows 8.x Store Apps this seems like how it should work. But it still doesn't work.
and this...
<ResourceDictionary Source="pack://application:,,,/{AssemblyName};component/Shared.xaml" />
(this is the WPF way, I know, but I thought I would give it a try anyway!)
But none seem to work...
The build action of the resource dictionaries that I have in my application assembly are set to 'Page'. These resource dictionaries are working simply using this in the merge:
<ResourceDictionary Source="Styles/Styles.xaml"/>
I get the following cryptic error:
Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source' because the type 'Windows.Foundation.String' cannot be assigned to the type 'Windows.Foundation.Uri'. [Line: 12 Position: 37]
As Romasz mentioned in comment, you need to reference the project which including the styles. And then using the following code to reference.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///UserControlLibs/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
XAML Merged Dictionaries are trickier than they seem. All is well if you are referencing a local project, then your Source= paths well.
If you are referencing an external DLL (not in-solution), the referenced DLL folder must also have all *.xml, *.xr.xml, *.xbf, *.jpg/png/gif, etc.
The procedure I follow is: 1. Reference DLL containing the merged dictionaries (XAML style sheets). 2. Ensure the reference path has all required files. 3. Add the merged dictionary reference to your App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///NAMESPACE_HERE/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
This PostBuild.bat file describes how I have VS copy all required files on successful build:
Echo Starting: PostBuildEvent: Call $(ProjectDir)PostBuild.Bat $(ProjectDir) $(OutDir) $(TargetPath) $(RootNameSpace)
Echo With Parameters: %1 %2 %3 %4
REM ***
REM *** Variables
REM ***
SET BuildLocationBin=..\..\..\..\..\..\..\..\bin
REM ***
Echo *** Publish to Bin
REM ***
MD %BuildLocationBin%
%WINDIR%\system32\attrib.exe %BuildLocationBin%\*.* -r /s
%WINDIR%\system32\xcopy.exe %1Properties\*.rd.xml %BuildLocationBin%\%4\Properties\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.png %BuildLocationBin%\%4\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.xbf %BuildLocationBin%\%4\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.xml %BuildLocationBin%\%4\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %3 %BuildLocationBin%\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.pri %BuildLocationBin%\*.* /s/r/y
Echo *** Postbuild Complete ***
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With