Uwp 如何在多个资源字典中使用公共资源

Uwp 如何在多个资源字典中使用公共资源,uwp,uwp-xaml,Uwp,Uwp Xaml,我想在多个资源字典中使用一些常用资源,如画笔、转换器等: <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Themes/Common.xaml" /> <ResourceDictionary S

我想在多个资源字典中使用一些常用资源,如画笔、转换器等:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Themes/Common.xaml" />
            <ResourceDictionary Source="/Themes/CustomStyles1.xaml" />
            <ResourceDictionary Source="/Themes/CustomStyles2.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

但是,每次我将Common.xaml添加到MergedDictionaries时,它都会加载Common.xaml,并且资源会被多次实例化。

关键是为什么要将
Common.xaml
引用到
CustomSyle1.xaml
字典

您有三个字典,更好的方法是每个字典包含不同的资源,并且它们之间没有依赖关系。因此,在页面中,您应该使用其中的一个或两个,将它们合并到页面资源中,并且可以使用所有合并的资源


如果确实要将
Common.xaml
引用到
CustomSyle1.xaml
,假设一旦需要在一个页面中使用
Common.xaml
以及
CustomStyle1.xaml
,您只需要将
CustomStyle1.xaml
合并到页面资源中,因为
Common.xaml
已经合并到
CustomSyle1.xaml
。在这种情况下,您将不会有多个实例化的谜题。

Common.xaml被称为Common,因为它包含公共资源。我需要从CustomStyle1.xaml、CustomStyle2.xaml等中引用它。。例如,在每个ResourceDictionary中,我希望使用名为MyIconColor的颜色资源,或者使用名为PaddingAll的控件填充-厚度资源的默认值
<!-- CustomStyles1.xaml -->
<ResourceDictionary
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Themes/Common.xaml" />
    </ResourceDictionary.MergedDictionaries>