深色/浅色主题资产限定符 [英] Dark/Light theme assets qualifiers

查看:30
本文介绍了深色/浅色主题资产限定符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的要求,即使用浅色/深色主题图像.我希望像

I have a very simple requirement to use Light/Dark themed images. I expected that a qualifier like

SomeImage.Theme-Light.png

或将图片放在名为 Theme-Light 的文件夹下

or putting the image under a folder named Theme-Light

Theme-Light/SomeImage.png

会起作用,而且确实起作用,但仅限于设计器模式.一旦我运行应用程序,即使正确设置了所需的主题(在应用程序和页面级别,因此所有其他 ThemeResources 都能正确加载),也会加载错误的图像.

would work, and it did, but only in the designer mode. As soon as I run the app, even though the required theme is properly set (on both app and page level so all the other ThemeResources get loaded correctly), wrong image gets loaded.

我知道为不同主题加载不同图像的解决方法,所以这不是我想要的.我很想知道为什么这种带有限定符的方法在运行时不起作用?是否应该使用不同的名称限定符?

I know about workarounds to load different images for different themes, so that's not what I'm looking for. I am curious to know why this approach with qualifiers doesn't work in runtime? Is there a different name qualifier that should be used?

我阅读了这篇文章:如何使用限定符命名资源 (XAML)" 但它仅显示了如何根据高对比度支持命名资产.

I read this article: "How to name resources using qualifiers (XAML)" but it only shows how to name the assets with regards to high contrast support.

推荐答案

这种方法不如限定符方便,但有效.

This aproach isn't as convenient as qualifiers, but it works.

在 App.xaml 中定义

Define in App.xaml

<ResourceDictionary>
    <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Light">
            <ImageSource x:Key="Logo">/Assets/Logo-White.png</ImageSource>
        </ResourceDictionary>

        <ResourceDictionary x:Key="Dark">
            <ImageSource x:Key="Logo">/Assets/Logo-Blue.png</ImageSource>
        </ResourceDictionary>

        <ResourceDictionary x:Key="HighContrast">
            <ImageSource x:Key="Logo">/Assets/Logo-White.png</ImageSource>
        </ResourceDictionary>
    </ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>

然后使用

<Image Source="{ThemeResource Logo}"/>

这篇关于深色/浅色主题资产限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆