Silverlight App.xaml中的"元素已经是另一个元素的子元素"错误

Ron*_*erg 8 silverlight xaml silverlight-3.0

我的App.xaml文件里面一直出现一个奇怪的错误:

元素已经是另一个元素的子元素.

我的App.xaml文件如下所示:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="Celerior.Annapurna.SL.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ProvisiorResourceDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

报告整个ResourceDictionary元素的错误(从第5行到第9行).

ProvisiorResourceDictionary.xaml包含许多样式和模板.在我看来没什么好激动的.

有谁知道什么是错的?

亲切的问候,

罗纳德威尔登伯格

Ron*_*erg 6

由于AnthonyWJones在评论中提供的提示,我自己找到了问题的原因.

看来Silverlight资源字典中的所有内容都必须是可共享的.原因是资源字典中的项目(可能)将添加到控件层次结构中的多个位置.

我的资源字典中有两个不可共享的项目.

编辑:在WPF中,您可以使用x:Shared资源字典中对象的属性来强制WPF为每个资源检索创建一个新实例.不幸的是,Silverlight 不支持此属性.

  • 提示:如果您不确定哪些项目导致此错误,只需创建App.xaml引用的第二个Resources2.xaml并将一些文件移到它上面.确保你完全重新编译.这应该允许您确定哪些资源是不可共享的并导致问题 (3认同)
  • 我遇到了这个错误,结果我们的一个开发人员在其中一个元素上放了`x:Name`而不是`x:Key`. (3认同)