WPF - 仅清除某些资源字典

发布于 2024-08-07 03:45:51 字数 186 浏览 3 评论 0原文

我通过清除所有合并字典 (Resources.MergedDictionaries.Clear()) 和基于所选主题的新字典,将“主题”应用到我的 WPF 应用程序。
我不想清除所有字典,而是只想清除某些“与主题相关”的字典,而让其他字典仍然加载。我怎样才能做到这一点?我在迭代字典时没有找到区分字典的方法...

感谢您的帮助!

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...

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2024-08-14 03:45:51

我假设您正在 Application 级别进行合并,否则您可以只引入一个中间控件,其唯一的工作是托管主题字典。在这种情况下,我建议使用多层方法,其中第一个合并的字典包含所有与主题相关的字典:

<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(...);

I'm assuming you're doing the merging at the Application level, otherwise you could just introduce an intermediate control whose only job is to host the theme dictionaries. That being the case, I would suggest using a multi-tiered approach whereby the first merged dictionary houses all theme-related dictionaries:

<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>

Now you can target only theme-related resources with code like this:

Application.Current.Resources.MergedDictionaries[0].Clear();
Application.Current.Resources.MergedDictionaries[0].Add(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文