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:
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?
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.
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.
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.
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.
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>
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>
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