23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

have to trick the app host into thinking that the source URI has changed by appending some dummy<br />

URI parameters. We can do this inside AccessibilitySettings.onhighcontrastchanged with<br />

eventArgs.target.highContrastScheme providing a decent variable for the URI (see js/scenario4.js in the<br />

modified sample):<br />

var page = WinJS.UI.Pages.define("/html/scenario4.html", {<br />

ready: function (element, options) {<br />

var accSet = new Windows.UI.ViewManagement.AccessibilitySettings();<br />

accSet.addEventListener("highcontrastchanged", function (e) {<br />

var image = document.getElementById("buttonImage");<br />

}<br />

});<br />

//Use the scheme name (sans whitespace) as the dummy URI parameter<br />

var params = e.target.highContrast ?<br />

"?" + e.target.highContrastScheme.replace(/\s*/g, "") : "";<br />

image.src = "../button.svg" + params;<br />

});<br />

One significant advantage to highcontrastchanged over media query listeners is that the latter will<br />

be fired very soon after the change happens, at which point the resource loader might not have picked<br />

up the change by the time you set the img.src attribute. This results in the wrong image being<br />

displayed. highcontrastchanged is fired much later, so the code above generally works. That said, my<br />

experiments along these lines (with the sample running in snap view and the desktop control panel in<br />

filled view) show that it’s still not 100% reliable: changing contrasts is an expensive operation that<br />

triggers many events throughout the system, and there’s no guarantee when the resource loader will<br />

get reset. For this reason you can consider just bypassing the whole matter and explicitly setting the src<br />

attribute to a known file with a specific name. The modified sample actually runs with code like this<br />

(commenting out the code above). Or you can just use media queries!<br />

Scale + Contrast = Resource Qualifiers<br />

Because the graphics we worked with in the previous section are SVGs, there is no need to supply<br />

separate files for different pixel densities. But what if we have raster graphics? How do we combine<br />

scaling and contrast? This will also come up when we look at localization in the next section, because we<br />

might also need to include language variants.<br />

This brings us to the matter of resource qualifiers, a topic that’s discussed in its fullest extent on How<br />

to name resources using qualifiers. Qualifiers include scale and contrast as we’ve seen, along with<br />

language, layout direction, home region, and a few other obscure variants.<br />

To combine qualifiers within a single filename, append them together with underscores. The general<br />

form is filename.qualifiername-value_qualifiername-value.ext. So, a graphic named logo.png can have<br />

variants like logo.contrast-high_scale-180.png and logo.scale-100_contrast-white.png (the order of<br />

qualifiers doesn’t matter). Clearly, with the full set of three or four scales (accounting for the few<br />

scale-80 cases) and four possible contrasts, you might have as many as 16 distinct graphics files for that<br />

794

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

Saved successfully!

Ooh no, something went wrong!