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.

control sample, and switch between the different high contrast themes in the Personalization section of<br />

Control Panel.<br />

Of course, an app will almost always have some layout of its own, such as div elements with custom<br />

color schemes and such defined in <strong>CSS</strong>. You’ll want to make sure you have appropriate style rules for<br />

high contrast settings, for which we have the -ms-high-contrast media feature for media queries,<br />

similar to -ms-view-state as we saw in Chapter 6. This feature can have the values of active (to apply its<br />

rules to all high contrast themes), black-on-white (the white background theme), white-on-black (a<br />

black background theme), and none. Clearly, none is implied when you don’t use -ms-high-contrast to<br />

group any rules; active is also implied if you use -ms-high-contrast without a value. We’ll take a closer<br />

look at all this in the next section.<br />

As with view states, you can use media query listeners and matchMedia to pick up contrast themes in<br />

code. This is useful for updating canvas elements, as we’ll see shortly. There is also the<br />

-ms-high-contrast-adjust <strong>CSS</strong> style that indicates whether to allow the element’s normal <strong>CSS</strong><br />

properties to be overridden for high contrast. The default value, auto, allows this; the value of none will<br />

prevent this behavior. Again, we’ll see more shortly.<br />

Next, WinRT surfaces the current contrast settings through the<br />

Windows.UI.ViewManagement.-AccessibilitySettings class. This has two properties: highContrast, a<br />

Boolean indicating if high contrast is on), and highContrastTheme, a string with the name of the high<br />

contrast color scheme. For the black on white theme this will be “High Contrast White”; for the other<br />

three themes in Control Panel > Personalization the strings will be “High Contrast #1”, “High Contrast<br />

#2”, and “High Contrast Black” (going from left to right). You can see these results through Scenario 2 of<br />

the UI contrast and settings sample, where the code is very simple:<br />

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

id("highContrast").inner<strong>HTML</strong> = accessibilitySettings.highContrast;<br />

id("highContrastScheme").inner<strong>HTML</strong> = accessibilitySettings.highContrast ?<br />

accessibilitySettings.highContrastScheme : "undefined";<br />

WinRT also provides detailed color information through the<br />

Windows.UI.ViewManagement.-UISettings.uIElementColor method. (Note the odd casing on<br />

uIElementColor, an artifact of WinRT names projecting into JavaScript.) This returns a<br />

Windows.UI.Color object for an element identified with a UIElementType. Scenario 1 of the UI contrast<br />

and settings sample shows all these possibilities with a piece of instructive but otherwise uninspiring<br />

code that I won’t duplicate here!<br />

The AccessibilitySettings object also supports one event, highcontrastchanged, that lets you<br />

know when high contrast is turned on or off; its eventArgs.target is the updated AccessbilitySettings<br />

object. You can use this event to trigger any programmatic updates you need to make in your UI, such<br />

as redrawing a canvas with high contrast colors if you’re not using a media query listener for that<br />

purpose.<br />

789

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

Saved successfully!

Ooh no, something went wrong!