English 中文(简体)
WPF - 仅清理某些资源单元
原标题:WPF - Clearing only certain Resource Dictionaries
  • 时间:2009-10-11 08:28:13
  •  标签:

I am applying "themes" to my WPF app by clearing all merged dictionaries (Resources.MergedDictionaries.Clear()) and new ones based on the selected theme.
Instead of clearing all dictionaries, I would like to clear only certain "theme-related" dictionaries, leaving others still loaded. How can I do that? I didn t find a way to differentiate dictionaries when iterating though them...

感谢你们的帮助!

问题回答

我假定你在<代码>应用<>代码/代码>上重新进行合并,否则,你只能实行中间控制,其唯一工作是主办主题词典。 因此,我建议采用多层次的方法,使第一个合并的字典能够容纳所有与主题有关的字典:

<Application.Resources>
    <!-- all application level resources -->
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- theme-related resources -->
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <!-- merge in theme-related dictionaries here -->
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>

            <!-- merge in other application-level dictionaries here -->
        </ResourceDictionary.MergedDictionaries>

        <!-- other resources -->
        <SolidColorBrush x:Key="Foo">Black</SolidColorBrush>
    </ResourceDictionary>
</Application.Resources>

现在,你只能把主题相关资源作为目标,其代码如下:

Application.Current.Resources.MergedDictionaries[0].Clear();
Application.Current.Resources.MergedDictionaries[0].Add(...);




相关问题
热门标签