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

时间:2015-11-15 16:21:08

标签: xaml windows-phone-8.1 high-contrast

我的应用中存在问题。在所有屏幕上,我都有硬编码的“白色”背景和文本颜色(前景)我使用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和(页面底部的“高对比度样本”) 而且什么都没有。 当我尝试使用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上有所不同。使用堆栈溢出链接的方法我得到相同的,我尝试给字典的“默认”或“HighContrast”键,但我不工作。我还找到了MSDN上所有密钥的列表,但PhoneAccentBrush不存在。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分: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主题中创建黑色。

答案 1 :(得分:0)

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

相关问题