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.

When sharing an image, don't forget to set the thumbnail for the DataPackage<br />

var streamReference =<br />

Windows.Storage.Streams.RandomAccessStreamReference.createFromFile(imageFile);<br />

request.data.properties.thumbnail = streamReference;<br />

request.data.setDataProvider(<br />

Windows.ApplicationModel.DataTransfer.StandardDataFormats.bitmap,<br />

onDeferredImageRequested);<br />

As indicated in the comments, it’s a really good idea to provide a thumbnail with delayed rendering<br />

so the target app has something to show the user. Then, when the target needs the full data, the data<br />

provider function gets called—in this case, onDeferredImageRequsted:<br />

function onDeferredImageRequested(request) {<br />

if (imageFile) {<br />

// Here we provide updated Bitmap data using delayed rendering<br />

var deferral = request.getDeferral();<br />

var imageDecoder, inMemoryStream;<br />

}<br />

}<br />

imageFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {<br />

// Decode the image<br />

return Windows.Graphics.Imaging.BitmapDecoder.createAsync(stream);<br />

}).then(function (decoder) {<br />

// Re-encode the image at 50% width and height<br />

inMemoryStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();<br />

imageDecoder = decoder;<br />

return Windows.Graphics.Imaging.BitmapEncoder.createForTranscodingAsync(<br />

inMemoryStream, decoder);<br />

}).then(function (encoder) {<br />

encoder.bitmapTransform.scaledWidth = imageDecoder.orientedPixelWidth * 0.5;<br />

encoder.bitmapTransform.scaledHeight = imageDecoder.orientedPixelHeight * 0.5;<br />

return encoder.flushAsync();<br />

}).done(function () {<br />

var streamReference = Windows.Storage.Streams.RandomAccessStreamReference<br />

.createFromStream(inMemoryStream);<br />

request.setData(streamReference);<br />

deferral.complete();<br />

}, function (e) {<br />

// didn't succeed, but we still need to release the deferral to avoid<br />

//a hang in the target app<br />

deferral.complete();<br />

});<br />

Note that this function receives a simplified hybrid of the DataRequest and DataPackage objects: a<br />

DataProviderRequest that contains deadline and formatId properties, a getDeferral method, and a<br />

setData method through which you provide the data that matched formatId. The deadline property, as<br />

you can guess, is the same as what the datarequested handler might have stored in the DataRequest<br />

object.<br />

501

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

Saved successfully!

Ooh no, something went wrong!