23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Canvas a better choice? To this point in the Here My Am! app, I’ve been using images to provide<br />

messages in img elements when a photograph or the map isn’t available. When considering the needs<br />

for contrast and localized variations of those images, it’s easier to take localized string resources, as<br />

we’ll work with later on, and just generate the images on the fly with a canvas. This eliminates the need<br />

for many different image files and make the app package smaller, while still fully addressing both<br />

accessibility and localization needs. The version of Here My Am! in this chapter now works this way.<br />

High Contrast Resources<br />

In the previous section with the <strong>CSS</strong> styling for high contrast mode sample we saw a bit of the filename<br />

conventions that the Windows resource loader uses for high contrast: button.contrast-high.svg, for<br />

example. Scenario 4 of that sample shows how this lookup can happen automatically. In the project<br />

there is a file named button.svg alongside one named button.contrast-high.svg, with an img element<br />

declared in html/scenario4.html as follows:<br />

<br />

If the system is running with normal contrast, the resource loader resolves the URI here to button.svg.<br />

(The ../ is because the scenario page is one level down in the <strong>HTML</strong> folder.) When high contrast is in<br />

effect, the resource loader instead looks for that same filename but with .contrast-high inserted before<br />

the extension.<br />

Note If you’re using custom app bar icons, as discussed in the “Custom Icons” section of Chapter 7,<br />

“Commanding UI,” remember to include high contrast variants of your source images using this naming<br />

scheme.<br />

If you like having more parallel filenames, you can also name the normal contrast file with<br />

.contrast-standard, as in button.contrast-standard.svg. If you do this in the sample project, leaving the<br />

<strong>HTML</strong> as is, you’ll see no difference in the output. At the same time, because of behavior nuances with<br />

contrast handling, it’s only recommended to use .contrast-standard if you also supply .contrast-white<br />

and .contrast-black variants.<br />

As noted before, these variants are applied automatically for black-on-white (white background) and<br />

white-on-black (black background) themes, respectively. To see this, make a copy of<br />

button.contrast-high.svg and name it button.contrast-white.svg, and then make a second copy names<br />

button.contrast-black.svg. In that second copy, modify the gradient colors in the CDATA block by<br />

exchanging black with ButtonFace. When you then switch on a black background theme, you’ll see a<br />

button that’s white on black, as it should be.<br />

All these changes can be found in the copy of the sample included with this chapter’s companion<br />

content.<br />

The one caveat with the img element in Scenario 4 is that it won’t be updated when contrast is<br />

changed while the app is running, as happens with media queries in Scenarios 1–3. That is, the app host<br />

will not re-render the img element in response to a contrast switch. To change this behavior, we basically<br />

793

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!