How can I import a deep hierarchy of merged dictionaries with 1 ResourceDictionary?

好久不见. 提交于 2019-12-23 17:04:38

问题


I am attempting to merge in dictionaries from a dependent class library project, but the resource keys can't be found. Note: I am using this Connect bug workaround from Microsoft which is supposed to allow the framework to search deep enough to find the nested resources. This does not appear to be working.

Example of Failure

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyApplication.ControlLibrary;component/ResourceDictionaries/ResourceLibrary.xaml" />
            <ResourceDictionary>
                <Style TargetType="{x:Type Line}" /> <!-- workaround from MS to allow for this -->
                <Main:AppBootstrapper x:Key="bootstrapper" /> <!-- CaliburnMicro bootstrapper, unsure if this is relevant -->
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Inside ResourceLibrary.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="DefaultColorTheme.xaml" />
        <!-- ...snip... -->
        <ResourceDictionary Source="TransitionControl.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

When doing this, it cannot find the resource keys. If I merge each dictionary in manually from that class library, it works fine. This, IMO, begins to defeat the purpose of abstracting resources out to an external assembly.

Example of Success

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyApplication.ControlLibrary;component/ResourceDictionaries/DefaultColorTheme.xaml" />
            <ResourceDictionary Source="/MyApplication.ControlLibrary;component/ResourceDictionaries/Images.xaml" />
            <ResourceDictionary Source="/MyApplication.ControlLibrary;component/ResourceDictionaries/FontIcons.xaml" />      

            <ResourceDictionary>
                <Style TargetType="{x:Type Line}" />
                <Main:AppBootstrapper x:Key="bootstrapper" />
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Have I placed the dummy implicit style in the wrong place? Something isn't adding up here. Thanks for looking.


回答1:


I've learned this the hard way by playing around with it for 3 days.

do not make really deep structures. Have a main dictionary that is just using other dictionaries. The app is not supposed to access anything from the others.

But the crucial thing is to reference them in the right order. it wont work if you load a RD where one of it's contents use something from another one that is not already loaded. The order is truly crucial.

Using WPF Inspector will help you a lot since it makes it possible to track everything donw in an WPF app.



来源:https://stackoverflow.com/questions/17750450/how-can-i-import-a-deep-hierarchy-of-merged-dictionaries-with-1-resourcedictiona

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!