Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic Dark/Light Icon Support in Windows Phone 8

I think this is a very common problem, but I cannot find a suitable solution for me. As you all know, WP supports a dark and a light theme. The user can change the theme and there are ways to override his decision and to display everything in the color theme you've selected. However, I'm just trying to react to this two theme types and I want to display icons in the correct color.

If you use the Application Bar, you can select from many built in icons, which will be automatically inverted from light to dark and vice versa.

Why isn't there any support for normal images? For example: I want to display a telephone icon. I've picked one from the built in icons and copied it from the Microsoft SDK folder to the Image folder of my project. If the user uses the dark theme, everything will be fine because the white telephone icon will be visible on the black background. But if he switches to the light theme, the icon will be invisible because it is white on white.

I'm fully aware of the style resources for textboxes or background colors, which use the phone's accent or theme color. But why is it, that there is no support for simple icons which I added as Image to my XAML page?

Of course I could detect in the constructor of the page if the user is in dark or light mode. I would then load either a black or white version of the telephone icon. But this check will be done everytime I visit the page and slows everything down. It's also annoying to manually add the check for the theme each time I'm adding a theme aware image.

Is there any solution, which will work with XAML only? Or is at least easy to maintain? And why can't I use the built in images from the SDK right from the beginning? They are already available in dark and light versions and are already used in the application bar.

like image 633
Markus Rudel Avatar asked Dec 20 '12 13:12

Markus Rudel


1 Answers

If you want the icon to function like in the actionbar, just be white (if dark theme) and black (if light theme) then you can add the image as an opacity mask to a rectangle, like this:

<Rectangle Fill="{StaticResource PhoneForegroundBrush}" Width="48" Height="48" >
    <Rectangle.OpacityMask>
        <ImageBrush ImageSource="/Images/my.icon.png" />
    </Rectangle.OpacityMask>
</Rectangle>

Where my.icon.png is a white image, like those you can choose for the actionbar.

like image 62
Johan Falk Avatar answered Sep 20 '22 02:09

Johan Falk