Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct syntax for splitting a generic.xaml file using MergedDictionaries (UWP)

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?

like image 668
Ladi Avatar asked Oct 23 '15 05:10

Ladi


People also ask

Where is generic XAML?

xaml is available in the \(Program Files)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\<SDK version>\Generic folder from a Windows Software Development Kit (SDK) installation.

What is ResourceDictionary?

A resource dictionary is a repository for XAML resources, such as styles, that your app uses. You define the resources in XAML and can then retrieve them in XAML using the {StaticResource} markup extension and {ThemeResource} markup extension s. You can also access resources with code, but that is less common.

How do I add a ResourceDictionary in XAML?

Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu. Here, you define a resource dictionary in a separate XAML file called Dictionary1.

What all we can store in a resource dictionary?

In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax.


2 Answers

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>
like image 190
Ladi Avatar answered Nov 07 '22 15:11

Ladi


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>
like image 39
Stam Avatar answered Nov 07 '22 15:11

Stam