Windows phone 7 在windows phone中引用合并的资源字典失败

Windows phone 7 在windows phone中引用合并的资源字典失败,windows-phone-7,xaml,mvvm-light,resourcedictionary,Windows Phone 7,Xaml,Mvvm Light,Resourcedictionary,我目前正在使用MVVMLight框架构建一个WP7应用程序。我想在app.xaml中添加一个资源字典,但是当我这样做失败时。这是app.xaml中的一个小漏洞 <Application.Resources> <!--Global View Model Locator--> <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

我目前正在使用MVVMLight框架构建一个WP7应用程序。我想在app.xaml中添加一个资源字典,但是当我这样做失败时。这是app.xaml中的一个小漏洞

<Application.Resources>
    <!--Global View Model Locator-->
    <vm:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True" />
    <!--Merged Resource Dictionaries-->
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="View/StyleResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

因为我使用的ViewModelLocator有一个键,所以会出现一个错误,警告我不能混合使用带键和不带键的资源。将密钥添加到我的资源字典后,它看起来如下所示:

    <ResourceDictionary x:Key="resourceDictionary">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="View/StyleResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
<Application.Resources>
    <!--Global View Model Locator-->
    <!--Merged Resource Dictionaries-->
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="View/StyleResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </ResourceDictionary>
</Application.Resources>

在资源字典中,我有一个带有“TitleTemplate”键的样式。在任何一种情况下,当我试图从我的一个视图引用资源字典时,它都失败了。我的观点中的示例代码如下:

<TextBlock Name="TB_ContactNameLabel" 
           Text="contact" 
           Style="{StaticResource TitleTemplate}"/>


设计师立即给我错误“无法解析资源'TitleTemplate'。如果我引用资源字典(即:resourceDictionary)的键,则不会抛出任何错误,但它显然不会做任何事情。最后,如果我将resourceDictionary直接添加到页面的参考资料中,而不是app.xaml,那么一切都可以正常工作。我不想将它添加到我计划使用的每个视图中。我在这里遗漏了什么吗?

您的应用程序资源应该如下所示:

    <ResourceDictionary x:Key="resourceDictionary">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="View/StyleResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
<Application.Resources>
    <!--Global View Model Locator-->
    <!--Merged Resource Dictionaries-->
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="View/StyleResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </ResourceDictionary>
</Application.Resources>


太棒了,这非常有效。但是你能解释为什么会这样吗?@ferics2:如果使用合并字典,资源字典必须是资源属性定义的单一根,并且所有资源都必须在其中定义。这就是它的工作方式。谢谢@Will。是的,当向现有词典添加另一个词典(合并)时,需要将其他资源放入词典中。