Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 10 Universal Merged Dictionaries

Tags:

xaml

uwp

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]

like image 278
emseetea Avatar asked Nov 13 '15 22:11

emseetea


2 Answers

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>

enter image description here

like image 182
Jeffrey Chen Avatar answered Nov 03 '22 01:11

Jeffrey Chen


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!

like image 26
Robert J. Good Avatar answered Nov 03 '22 02:11

Robert J. Good