如何在Page.Resources中包含两个资源?

tot*_*oro 5 c# wpf xaml

在WPF中,这曾经很好用:

<Page.Resources>
    <ResourceDictionary Source="resources/Styles.xaml" />
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)

但添加转换器(见下文)会导致第二个资源(Style.xaml)的错误:Each dictionary entry must have an associated key.

<Page.Resources>
    <local:MySizeConverter x:Key="sizeConverter"/>
    <ResourceDictionary Source="resources/Styles.xaml" />
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)

但是,将键添加到第二行(例如<ResourceDictionary x:Key="myStyleDict" Source="resources/Styles.xaml" />,在后面的代码中导致以下错误

The name 'aTextBlockUsedToWork' does not exist in the current context
Run Code Online (Sandbox Code Playgroud)

在那里aTextBlockUsedToWork可以在后面的代码添加键之前成功访问.请注意,如果我注释掉样式资源,转换器工作正常.我怎样才能让这两种资源都有效?

Yog*_*esh 10

您需要使用MergedDictionaries导入另一个字典文件,如下所示:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="resources/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <local:MySizeConverter x:Key="sizeConverter"/>
    </ResourceDictionary>
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)