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.

var images = iFrame.contentDocument.documentElement.getElementsByTagName("img");<br />

if (images.length > 0) {<br />

shareOperation.data.getResourceMapAsync().done(function (resourceMap) {<br />

if (resourceMap.size > 0) {<br />

for (var i = 0, len = images.length; i < len; i++) {<br />

var streamReference = resourceMap[images[i].getAttribute("src")];<br />

if (streamReference) {<br />

// Call a helper function to map the image element's src<br />

// to a corresponding blob URL generated from the streamReference<br />

setResourceMapURL(streamReference, images[i]);<br />

}<br />

}<br />

}<br />

});<br />

}<br />

The setResourceMapURL helper function does pretty much what the bitmap-specific code did, which<br />

is call openReadAsync on the stream, call MSApp.createBlobFromRandomAccessStream, pass that blob to<br />

URL.createObjectURL, set the img.src with the result, and close the stream.<br />

After the target app has completed a sharing operation, it should call the ShareOperation.report-<br />

Completed method, as shown earlier with the template code. This lets the system know that the data<br />

package has been consumed, the share flow is complete, and all related resources can be released. The<br />

share target sample does this when you explicitly click a button for this purpose, but normally you<br />

automatically call the method whenever you’ve completed the share. Do be aware that calling<br />

reportCompleted will close the target app’s sharing UI, so avoid calling it as soon as the target activates:<br />

you want the user to feel confident that the operation was carried out.<br />

Long-Running Operations<br />

When you run the share target sample and invoke the Share charm from a suitable source app, there’s a<br />

little expansion control near the bottom labeled “Long-running Share support.” If you expand that,<br />

you’ll see some additional controls and a bunch of descriptive text, as shown in Figure 12-6. The buttons<br />

shown here tie into a number of other methods on the ShareOperation object alongside<br />

reportCompleted. These help Windows understand exactly how the share operation is happening within<br />

the target: reportStarted, reportDataRetrieved, reportSubmittedBackgroundTask, and reportError. As<br />

you can see from the descriptions in Figure 12-6, these generally relate to telling Windows when the<br />

target app has finished cooking its meal, so to speak, and the system can clean the dishes and put away<br />

the utensils:<br />

• reportStarted informs Windows that your sharing operation might take a while, as if you’re<br />

uploading the data from the package to another place, or just sending an email attachment with<br />

what ends up being large images and such. This specific method indicates that you’ve obtained<br />

all necessary user input and that the share pane can be dismissed.<br />

• reportDataRetrieved informs Windows that you’ve extracted what you need from the data<br />

package such that it can be released. If you’ve called MSApp.createBlobFromRandomAccessStream<br />

for an image stream, for example, the blob now contains a copy of the image that’s local to the<br />

508

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

Saved successfully!

Ooh no, something went wrong!