【问题标题】:What is the correct syntax for splitting a generic.xaml file using MergedDictionaries (UWP)使用 MergedDictionaries (UWP) 拆分 generic.xaml 文件的正确语法是什么
【发布时间】:2015-10-23 05:35:17
【问题描述】:

考虑我创建包含一组自定义控件的库MyCustomControlsProject 的情况。我不想将所有这些控件的 XAML 代码放在一个非常大的 generic.xaml 中,而是将每个控件分隔在其自己的 XAML 文件中,然后从 generic.xaml 引用该文件

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

解决方案资源管理器(以及文件系统)中的文件夹结构如下所示:

  • MyCustomControlsProject(项目/文件夹)
    • 主题(文件夹)
      • 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 还没有赶上的东西?

【问题讨论】:

  • 您是否已将库项目引用添加到您的应用项目中?尝试删除并重新添加引用,然后重试。如果问题仍然存在,您能否分享一个可以在 GitHub 或在线文件共享上重现该问题的示例。
  • 您是在每个MyControl.xaml 文件中定义了UserControl,还是只是为控件定义了数据模板?甚至是带有绑定的数据模板?

标签: xaml custom-controls windows-10 uwp


【解决方案1】:

正确的语法是:

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

【讨论】:

    【解决方案2】:

    您需要在最后一次尝试中添加 Themes 文件夹:

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

    【讨论】:

    • 产生同样的错误。另外,请注意文件 Generic.xaml 位于 Themes 内,因此具有您希望使用“ControlTemplates/MyControl1.xaml”而不是“Themes/ControlTemplates/MyControl1.xaml”的相对路径 另请注意,异常提示源不能转换为 Uri。我想知道除了简单的 Uri 不指向文件之外,是否还有其他东西。异常提示字符串格式不正确。
    • 对不起,我以为您将它们全部添加到 App.xaml 中。所以你不需要主题。但即使这样,它也对我有用。您是否在 App.xaml &lt;ResourceDictionary Source="Themes/Generic.xaml" /&gt; 中尝试过?
    • 我没有 app.xaml,因为我在单独的库项目中创建这些控件。我不想让那些在他们的解决方案中包含包含自定义控件的项目的人编辑他们的 app.xaml;我希望单独的项目是自包含的。如果合并的字典可以在 generic.xaml 中使用,那么我的问题就解决了。这在所有过去的 Silverlight 或 Universal 版本中都是可能的,尽管在指定 URL 的方式上存在小的语法差异。
    猜你喜欢
    • 1970-01-01
    • 2010-10-15
    • 2020-11-22
    • 2017-05-02
    • 2013-11-21
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多