使用 MergedDictionaries (UWP) 拆分 generic.xaml 文件的正确语法是什么
What is the correct syntax for splitting a generic.xaml file using MergedDictionaries (UWP)
考虑我创建一个包含一组自定义控件的库 MyCustomControlsProject
的情况。我不想将所有这些控件的 XAML 代码放在一个非常大的 generic.xaml 中,而是想将每个控件分开放在它自己的 XAML 文件中,然后从 generic.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="<url_syntax_file_1>" />
<ResourceDictionary Source="<url_syntax_file_2>" />
</ResourceDictionary.MergedDictionaries>
解决方案资源管理器(以及文件系统)中的文件夹结构如下所示:
- 我的自定义控件项目 (project/folder)
- 主题(文件夹)
- Generic.xaml(文件)
- ControlTemplates(文件夹)
- MyControl1.xaml(文件)
- MyControl2.xaml(文件)
过去,我在 Silverlight 和 Silverlight for Win Phone 中使用此语法执行此操作:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyCustomControlsProject;Component/Themes/ControlTemplates/MyControl1.xaml"/>
<ResourceDictionary Source="/MyCustomControlsProject;Component/Themes/ControlTemplates/MyControl2.xaml"/>
</ResourceDictionary.MergedDictionaries>
并且对于 Windows Phone 8.1 使用此语法:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Themes/ControlTemplates/MyControl1.xaml" />
<ResourceDictionary Source="ms-appx:///Themes/ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>
这些语法都不适用于 Win 10 (UWP)。尝试使用这些会导致 运行 时间异常:
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'.
我也尝试过导致相同异常的语法:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ControlTemplates/MyControl1.xaml" />
<ResourceDictionary Source="ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>
有趣的是 app.xaml 使用上面的语法似乎没有问题。
有人知道 generic.xaml 中 ResourceDictionary
节点的 source
属性中 url 字符串的正确语法吗?或者这是 UWP 还没有赶上的东西?
您需要在上次尝试添加 Themes 文件夹:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/ControlTemplates/MyControl1.xaml" />
<ResourceDictionary Source="Themes/ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
正确的语法是:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///MyCustomControlsProject/Themes/ControlTemplates/MyControl1.xaml" />
<ResourceDictionary Source="ms-appx:///MyCustomControlsProject/Themes/ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>
考虑我创建一个包含一组自定义控件的库 MyCustomControlsProject
的情况。我不想将所有这些控件的 XAML 代码放在一个非常大的 generic.xaml 中,而是想将每个控件分开放在它自己的 XAML 文件中,然后从 generic.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="<url_syntax_file_1>" />
<ResourceDictionary Source="<url_syntax_file_2>" />
</ResourceDictionary.MergedDictionaries>
解决方案资源管理器(以及文件系统)中的文件夹结构如下所示:
- 我的自定义控件项目 (project/folder)
- 主题(文件夹)
- Generic.xaml(文件)
- ControlTemplates(文件夹)
- MyControl1.xaml(文件)
- MyControl2.xaml(文件)
- 主题(文件夹)
过去,我在 Silverlight 和 Silverlight for Win Phone 中使用此语法执行此操作:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyCustomControlsProject;Component/Themes/ControlTemplates/MyControl1.xaml"/>
<ResourceDictionary Source="/MyCustomControlsProject;Component/Themes/ControlTemplates/MyControl2.xaml"/>
</ResourceDictionary.MergedDictionaries>
并且对于 Windows Phone 8.1 使用此语法:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Themes/ControlTemplates/MyControl1.xaml" />
<ResourceDictionary Source="ms-appx:///Themes/ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>
这些语法都不适用于 Win 10 (UWP)。尝试使用这些会导致 运行 时间异常:
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'.
我也尝试过导致相同异常的语法:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ControlTemplates/MyControl1.xaml" />
<ResourceDictionary Source="ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>
有趣的是 app.xaml 使用上面的语法似乎没有问题。
有人知道 generic.xaml 中 ResourceDictionary
节点的 source
属性中 url 字符串的正确语法吗?或者这是 UWP 还没有赶上的东西?
您需要在上次尝试添加 Themes 文件夹:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/ControlTemplates/MyControl1.xaml" />
<ResourceDictionary Source="Themes/ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
正确的语法是:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///MyCustomControlsProject/Themes/ControlTemplates/MyControl1.xaml" />
<ResourceDictionary Source="ms-appx:///MyCustomControlsProject/Themes/ControlTemplates/MyControl2.xaml" />
</ResourceDictionary.MergedDictionaries>