7

Consider the case where I create a library MyCustomControlsProject containing a set of custom controls. Instead of placing the XAML code for all those controls in a very large generic.xaml I want to separate each control in its own XAML file and then reference that file from generic.xaml

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="<url_syntax_file_1>" />
    <ResourceDictionary Source="<url_syntax_file_2>" />
</ResourceDictionary.MergedDictionaries>

The folder structure in the solution explorer (as well as on the file system) looks like this:

  • MyCustomControlsProject (project/folder)
    • Themes (folder)
      • Generic.xaml (file)
      • ControlTemplates (folder)
        • MyControl1.xaml (file)
        • MyControl2.xaml (file)

In the past, I did this in Silverlight and in Silverlight for Win Phone using this syntax:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/MyCustomControlsProject;Component/Themes/ControlTemplates/MyControl1.xaml"/>
    <ResourceDictionary Source="/MyCustomControlsProject;Component/Themes/ControlTemplates/MyControl2.xaml"/>
</ResourceDictionary.MergedDictionaries>

And for Windows Phone 8.1 using this syntax:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="ms-appx:///Themes/ControlTemplates/MyControl1.xaml" />
    <ResourceDictionary Source="ms-appx:///Themes/ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>

Neither of these syntaxes works in Win 10 (UWP). Attempting to use those leads to a run time exception:

An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in MyApplication.exe but was not handled in user code
WinRT information: 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'.

I also tried this syntax that resulted in the same exception:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="ControlTemplates/MyControl1.xaml" />
    <ResourceDictionary Source="ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>

Interestingly enough it seems that app.xaml has no problems using the syntax above.

Does anyone know the correct syntax for the url string in a source attribute in a ResourceDictionary node in generic.xaml? Or is this something that UWP did not catch up with yet?

2
  • Have you added the library project reference to your app project? Try to remove and re-add the reference, and try again. If issue persists, can you share a sample which can reproduce the issue on GitHub or online file share. Oct 23, 2015 at 8:05
  • Did you define a UserControl in each MyControl.xaml file, or just data templates for controls? Or even data templates with bindings?
    – Herdo
    Oct 23, 2015 at 9:44

2 Answers 2

7

The correct syntax is:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="ms-appx:///MyCustomControlsProject/Themes/ControlTemplates/MyControl1.xaml" />
    <ResourceDictionary Source="ms-appx:///MyCustomControlsProject/Themes/ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>
1

What you need is to add the Themes folder in your last try:

<ResourceDictionary>  
  <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Themes/ControlTemplates/MyControl1.xaml" />
        <ResourceDictionary Source="Themes/ControlTemplates/MyControl2.xaml" />
    </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>
3
  • Produces the same error. Also, please note that the file Generic.xaml is inside Themes so with a relative path like that you'd expect to use "ControlTemplates/MyControl1.xaml" and not "Themes/ControlTemplates/MyControl1.xaml" Also note that the exception suggests that the source cannot be converted to a Uri. I wonder if there is not something more than simply the Uri not pointing to a file. The exception suggests that the string format is incorrect.
    – Ladi
    Oct 23, 2015 at 7:01
  • Sorry I thought that you add them all in the App.xaml. So you don't need Themes. But even by that way it works for me. Did you try in your App.xaml <ResourceDictionary Source="Themes/Generic.xaml" /> ?
    – Stam
    Oct 23, 2015 at 7:37
  • I do not have an app.xaml since I am creating those controls in a separate library project. I do not want to ask people who include in their solution the project containing the custom controls to edit their app.xaml; I'd like the separate project to be self contained. If the merged dictionary can be used in generic.xaml then my problem is solved. That was possible in all past Silverlight or Universal versions albeit with small syntax differences in the way the URL was specified.
    – Ladi
    Oct 23, 2015 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.