Automatic Dark/Light Icon Support in Windows Phone

2019-03-14 13:41发布

问题:

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.

回答1:

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.



回答2:

You could use vector graphics instead of bitmap icons and use a theme-aware brush to draw them.

If you want the App to respond to a switch of the theme you'll need to respond to it any way.

I added a property to the base class of my Views that returns the selected theme. That way I can use/bind to that value.

It is also possible to use a ValueConverter that turns a logical name of a resource into a name of a theme specific resource.

Edit

Have a look at this: Custom light/dark theme resources on Windows Phone 7



回答3:

Detect the theme (Supporting dark and light themes gives the best solution for this, I think), and then set the image accordingly. If you do this a lot, a custom control where you can supply two image sources and the correct one gets used would be easy enough to create.

Edit: Here's another good article on this topic. New Screen Resolutions



回答4:

You pick a White Foregrounded icon use it. It suits for both Light and Dark themes. Thats what I have been doing.



回答5:

The Coding 4 Fun control toolkit includes a round button that mimics the application bar buttons, including updating the foreground color depending on the theme. The code is open source, so perhaps you might find an answer in there. Or, as a hack, you could use the RoundButton control, turn off the border, and not provide a Click event.