xamlwindows-phone-8.1high-contrast

How to override PhoneAccentBrush on High Contrast WP8.1


I have a problem in my app. On all screens I have hard-coded "White" Background and for text color (Foreground) I use PhoneAccentBrush. Everything was fine before I check high-contrast option in ease of access. Then PhoneAccentBrush turns white, and I end up with white text on white background. I check how can I solve the problem and it seems to me that the best option is overriding PhoneAccentBrush in high-contrast mode (I also thought about converter, but I have to much text to use it everywhere). Basically all I want to do is set PhoneAccentBrush black in high-contrast. I fight with this for hours now and still have nothing. I check this: How to ignore "ease of access" settings on Windows Phone? https://msdn.microsoft.com/library/windows/apps/br208807?f=255&MSPPError=-2147217396 and ("High contrast sample" on the bottom of page) and still have nothing. When I try to use MSDN example it doesn't work on WP (it's preparend for full Windows) - when I paste this code into app.xaml

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

    </ResourceDictionary>
</Application.Resources>

I get errors: "Each dictionary entry must have an associated key." "Dictionary Item 'ResourceDictionary' must have a Key attribute" so it's different on Windows Phone. With method from stack overflow link I get the same, I try to give "Default" or "HighContrast" key for Dictionary, but I doesn't work. Also I found list of all keys on MSDN but PhoneAccentBrush isn't there. Somebody can help me?


Solution

  • You need to create a themed resource dictionary, in your app.xaml file, where you set your foreground brush for different themes:

    <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>
    

    Notice how MyForegroundBrush uses the PhoneAccentBrush in the dark/light themes, but is hardcoded to an appropiate colour in the high contrast modes.

    In your XAML just use MyForegroundBrush as normal, and the correct version will be used depending on the current theme.

    <TextBlock Foreground="{ThemeResource MyForegroundBrush}">
    

    Just a thought, though, if a user has set their theme to high contrast black they generally want a black background. Is hard coding it to white in your app the right thing to do? You could use the above approach to create a background brush that is white in the Light/High Contrast White themes, but black in the High Contrast Black theme.