如何在高对比度 WP8.1 上覆盖 PhoneAccentBrush

How to override PhoneAccentBrush on High Contrast WP8.1

我的应用程序有问题。在所有屏幕上,我都对 "White" 背景进行了硬编码,对于文本颜色(前景),我使用了 PhoneAccentBrush。在我检查易于访问的高对比度选项之前,一切都很好。然后 PhoneAccentBrush 变成白色,我最终得到白色背景上的白色文本。我检查了如何解决这个问题,在我看来最好的选择是在高对比度模式下覆盖 PhoneAccentBrush(我也考虑过转换器,但我必须有很多文本才能在任何地方使用它)。基本上我想做的就是在高对比度下将 PhoneAccentBrush 设置为黑色。我现在为此奋斗了几个小时,但仍然一无所获。我检查这个: How to ignore "ease of access" settings on Windows Phone? https://msdn.microsoft.com/library/windows/apps/br208807?f=255&MSPPError=-2147217396 和(页面底部的 "High contrast sample") 仍然一无所有。 当我尝试使用 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>

我收到错误: "Each dictionary entry must have an associated key." "Dictionary Item 'ResourceDictionary' must have a Key attribute" 所以在 Windows Phone 上是不同的。使用堆栈溢出 link 中的方法,我得到了相同的结果,我尝试为字典提供 "Default" 或 "HighContrast" 键,但我不起作用。我还在 MSDN 上找到了所有键的列表,但没有 PhoneAccentBrush。有人可以帮助我吗?

您需要在 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 如何在 dark/light 主题中使用 PhoneAccentBrush,但在高对比度模式下硬编码为适当的颜色。

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

<TextBlock Foreground="{ThemeResource MyForegroundBrush}">

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

我也遇到了资源字典中缺失的PhoneAccentBrush。结果我删除了指向资源字典的默认页面背景,当我把它放回去时它起作用了:Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"