Referencing a merged resource dictionary in window

2019-02-24 00:25发布

I am currently building a WP7 app using the MVVMLight framework. I would like to add a resource dictionary to my app.xaml, however when I do it fails. Here is a snipit from the 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>

Because I am using a ViewModelLocator that has a key, I get an error warning me that I can not mix resources with and without keys. After adding a key to my resource dictionary It looks like the following:

    <ResourceDictionary x:Key="resourceDictionary">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="View/StyleResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

In the resource dictionary I have a style with the key "TitleTemplate". In either case when I try to reference the resource dictionary from one of my views it fails. Sample code from my view is bellow:

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

The designer immediately gives me the error "The resource 'TitleTemplate' could not be resolved". If I reference the key of the resourced dictionary (ie: resourceDictionary) no error is thrown but it doesn't do anything obviously. Finally if I add the resourceDictionary directly to the page in its resources, instead of the app.xaml everything works fine. I do not want to have to add it to each view I plan to use. Am I missing something here?

1条回答
聊天终结者
2楼-- · 2019-02-24 00:33

Your application resources should look like the following:

<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>
查看更多
登录 后发表回答