7

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]

Azure Terraformer
  • 1,648
  • 1
  • 14
  • 33
  • 2
    Have you added your library as a reference to the project? I'm using `` and it seems to work without problems. – Romasz Nov 13 '15 at 22:35
  • How are you adding this resource dictionary to the merged dictionary? What happens then? – Igor Ralic Nov 13 '15 at 23:28
  • Yes, {AssemblyName} is added as a reference to my application's project :) – Azure Terraformer Nov 14 '15 at 01:30
  • What do you guys have the build action set to on the resource dictionary in the other assembly? – Azure Terraformer Nov 14 '15 at 01:35
  • OK, it appears that it does work with what you suggested Romasz. I'm not sure how but I suspect when I was trying all the different combinations of build action / Source URI I must've set the build action to something other than Page when I was trying that technique. – Azure Terraformer Nov 14 '15 at 01:50

2 Answers2

8

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

Jeffrey Chen
  • 4,650
  • 1
  • 18
  • 22
  • In the example you gave, let's assume you want UserControlLibs to be a nugget package. In that case, how can you merge the resource dictionary? – Azure Terraformer Dec 03 '15 at 18:33
1

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!

Robert J. Good
  • 1,307
  • 10
  • 8
  • 1
    I found out that checking "Generate library layout", an option you'll find in Properties | Build, actually places the files to the right locations. – ericdes May 19 '16 at 05:23