1

我的应用程序有问题。在所有屏幕上,我都有硬编码的“白色”背景,对于文本颜色(前景),我使用 PhoneAccentBrush。在我检查高对比度选项以方便访问之前,一切都很好。然后 PhoneAccentBrush 变成白色,我最终在白色背景上显示白色文本。我检查了如何解决问题,在我看来,最好的选择是在高对比度模式下覆盖 PhoneAccentBrush(我也考虑过转换器,但我必须有很多文本才能在任何地方使用它)。基本上我想要做的就是将PhoneAccentBrush 设置为高对比度的黑色。我现在和这个斗争了几个小时,但仍然一无所有。我检查一下: 如何忽略 Windows Phone 上的“轻松访问”设置? https://msdn.microsoft.com/library/windows/apps/br208807?f=255&MSPPError=-2147217396和(页面底部的“高对比度示例”)仍然没有。当我尝试使用 MSDN 示例时,它在 WP 上不起作用(它为完整的 Windows 做好了准备) - 当我将此代码粘贴到 app.xaml 中时

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Common/StandardStyles.xaml"/>
            <ResourceDictionary Source="Sample-Utils/SampleTemplateStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

我收到错误:“每个字典条目都必须有一个关联的键。” “字典项 'ResourceDictionary' 必须具有 Key 属性”,因此它在 Windows Phone 上有所不同。使用来自堆栈溢出链接的方法,我得到相同的结果,我尝试为 Dictionary 提供“Default”或“HighContrast”键,但我不起作用。我还在 MSDN 上找到了所有键的列表,但 PhoneAccentBrush 不存在。有人可以帮助我吗?

4

2 回答 2

3

您需要在 app.xaml 文件中创建主题资源字典,在其中为不同主题设置前景画笔:

<Application.Resources>
    <ResourceDictionary>
        <!-- Non-themed resources -->
        <SolidColorBrush x:Key="PivotHeaderForegroundSelectedBrush" Color="DarkBlue" />
        <SolidColorBrush x:Key="PivotHeaderForegroundUnselectedBrush" Color="Gray" />

        <!-- Themed resources -->
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Dark">
                <SolidColorBrush x:Key="MyForegroundBrush" Color="{Binding Source={ThemeResource PhoneAccentBrush}, Path=Color}"/>
            </ResourceDictionary>

            <ResourceDictionary x:Key="Light">
                <SolidColorBrush x:Key="MyForegroundBrush" Color="{Binding Source={ThemeResource PhoneAccentBrush}, Path=Color}"/>
            </ResourceDictionary>

            <ResourceDictionary x:Key="HighContrastBlack">
                <SolidColorBrush x:Key="MyForegroundBrush" Color="Black"/>
            </ResourceDictionary>

            <ResourceDictionary x:Key="HighContrastWhite">
                <SolidColorBrush x:Key="MyForegroundBrush" Color="Black"/>
            </ResourceDictionary>

            <ResourceDictionary x:Key="HighContrastCustom">
                <SolidColorBrush x:Key="MyForegroundBrush" Color="Black"/>
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Application.Resources>

注意 MyForegroundBrush 如何在深色/浅色主题中使用 PhoneAccentBrush,但在高对比度模式下被硬编码为适当的颜色。

在您的 XAML 中,只需照常使用 MyForegroundBrush,将根据当前主题使用正确的版本。

<TextBlock Foreground="{ThemeResource MyForegroundBrush}">

不过,只是一个想法,如果用户将他们的主题设置为高对比度黑色,他们通常想要黑色背景。在您的应用程序中将其硬编码为白色是否正确?您可以使用上述方法创建在 Light/High Contrast White 主题中为白色但在 High Contrast Black 主题中为黑色的背景画笔。

于 2015-11-16T09:27:25.580 回答
0

我也经历过资源字典中缺少的PhoneAccentBrush。原来我删除了指向资源字典的默认页面背景,当我把它放回去时它起作用了:Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"

于 2016-03-18T18:24:32.037 回答