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 key here is the args.detail.shareOperation object provided to the activated handler. This is a<br />

Windows.ApplicationModel.DataTransfer.ShareTarget.ShareOperation object, whose data<br />

property contains a read-only package called a DataPackageView from which you obtain all the goods:<br />

• To check whether the package has formats you can consume, use the contains method or the<br />

availableFormats collection.<br />

• To obtain data from the package, use its get* methods such as getTextAsync, getBitmap-Async,<br />

and getDataAsync (for custom formats). When pasting <strong>HTML</strong> you can also use the<br />

getResourceMapAsync method to get relative resource URIs. The view’s properties like the<br />

thumbnail are also useful to provide a preview of the data.<br />

As you can see, the Share target item template code above doesn’t do anything with shared data<br />

other than display the title, description, and thumbnail; clearly your app will do something more by<br />

requesting data from the package, like the examples we see in the share target sample. Its js/target.js<br />

file contains an activated handler for the target.html page (in the project root), and it also displays the<br />

thumbnail in the data package by default. It then looks for different data formats and displays those<br />

contents if they exist:<br />

if (shareOperation.data.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.<br />

text)) {<br />

shareOperation.data.getTextAsync().done(function (text) {<br />

displayContent("Text: ", text, false);<br />

});<br />

}<br />

The same kind of code appears for the simpler formats. Consuming a bitmap is a little more work but<br />

straightforward:<br />

if (shareOperation.data.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.<br />

bitmap)) {<br />

shareOperation.data.getBitmapAsync().done(function (bitmapStreamReference) {<br />

bitmapStreamReference.openReadAsync().done(function (bitmapStream) {<br />

if (bitmapStream) {<br />

var blob = MSApp.createBlobFromRandomAccessStream(bitmapStream.contentType,<br />

bitmapStream);<br />

document.getElementById("imageHolder").src = URL.createObjectURL(blob,<br />

{ oneTimeOnly: true });<br />

document.getElementById("imageArea").className = "unhidden";<br />

}<br />

});<br />

});<br />

}<br />

For <strong>HTML</strong>, it looks through the markup for img elements and then sets up their src attributes from<br />

the resource map (the iframe already has the <strong>HTML</strong> content from the package by this time):<br />

507

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

Saved successfully!

Ooh no, something went wrong!