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.

The library makes it easier to populate a template through object properties rather than XmlDocument<br />

methods, and because it’s been very well-tested within Microsoft it’s a more robust approach than<br />

creating an XmlDocument directly from strings. Here’s how it’s used in Scenario 1 to create, once again,<br />

the same payload we’ve already seen:<br />

function sendTileTextNotification() {<br />

var tileContent =<br />

NotificationsExtensions.TileContent.TileContentFactory.createTileWideText03();<br />

tileContent.textHeadingWrap.text = "Hello World! My very own tile notification";<br />

var squareTileContent = NotificationsExtensions.TileContent.TileContentFactory<br />

.createTileSquareText04();<br />

squareTileContent.textBodyWrap.text = "Hello World! My very own tile notification";<br />

tileContent.squareContent = squareTileContent;<br />

}<br />

Windows.UI.Notifications.TileUpdateManager.createTileUpdaterForApplication()<br />

.update(tileContent.createNotification());<br />

Simply said, the library’s TileContentFactory object provides methods to create objects equivalent<br />

to the XML documents provided by TileUpdateManager.getTemplateContent. As shown in the code<br />

above, those objects have properties equivalent to each field in the template, and when you’re ready to<br />

pass it to TileUpdater.update, you just call its createNotification method.<br />

The other reason this library exists is to simplify the process of creating an ASP.NET web service for<br />

periodic updates and push notifications (where the latter can send tile updates, badge updates, and<br />

toast notifications). Instead of creating the XML payloads manually—a fragile and highly error-prone<br />

practice at best—the service can use the Notifications Extensions Library to easily and consistently<br />

create the XML for all these notifications.<br />

Because the object model in the library clearly describes the XML, it’s fairly easy to use. There is also a<br />

topic in the documentation for it called Quickstart: Using the NotificationsExtensions library in your<br />

code. The samples we look at in this chapter also show most use cases.<br />

Using Local and Web Images<br />

Scenario 1 of the sample, as we’ve seen, shows tile updates using text, but the more interesting ones<br />

include graphics as well. These can come either from the app package, local app data, or the web, using<br />

ms-appx:///, ms-appdata:///local, and http:// URIs, respectively. These URIs are simply assigned to<br />

the src attributes of image elements within the tile templates. (These are image, not img as in <strong>HTML</strong>.)<br />

Note again that the first two URIs typically have three slashes at the beginning to denote “the current<br />

app”; http:// URIs also require that the Internet (Client) capability be declared in the app’s manifest.<br />

Scenario 2 of the sample (js/sendLocalImage.js) shows the use of ms-appx:/// for images within the<br />

app package, with variants for all three methods we’ve just seen to create the payload. When using<br />

XmlDocument methods, setting an image source looks like this:<br />

var tileImageAttributes = tileXml.getElementsByTagName("image");<br />

tileImageAttributes[0].setAttribute("src", "ms-appx:///images/redWide.png");<br />

582

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

Saved successfully!

Ooh no, something went wrong!