黑暗/光明主题资产限定符

时间:2016-03-09 17:22:06

标签: c# xaml win-universal-app

我有一个非常简单的要求,使用浅色/深色主题图像。我期待像

这样的限定符
SomeImage.Theme-Light.png

或将图像放在名为Theme-Light

的文件夹下
Theme-Light/SomeImage.png

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

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

我读过这篇文章:" How to name resources using qualifiers (XAML)"但它只展示了如何根据高对比度支持命名资产。

1 个答案:

答案 0 :(得分:2)

这种方法与限定符一样方便,但它有效。

在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}"/>