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 source app sample provides a number of scenarios that demonstrate how to share different<br />

types of data. They also show how to programmatically invoke the Share charm. This isn’t typically<br />

recommended, but it is possible. If it really fits in your app scenario, here’s how:<br />

Windows.ApplicationModel.DataTransfer.DataTransferManager.showShareUI();<br />

Calling this will, as when the user invokes the charm, trigger the datarequested event where<br />

eventArgs.request object is a Windows.ApplicationModel.DataTransfer.DataRequest object. This<br />

request object contains two properties and two methods:<br />

• data is the DataPackage to populate. It contains methods to make various data formats available,<br />

though it’s important to note that not all formats will be immediately rendered. Instead, they’re<br />

rendered only when a share target asks for them.<br />

• deadline is a Date property indicating the time in the future when the data you’re making<br />

available will no longer be valid (that is, will not render). This recognizes that there might be an<br />

indeterminate amount of time between when the source app is asked for data and when the<br />

target actually tries to use it. With delayed rendering, as noted above for the data property, it’s<br />

possible that some transient source data might disappear after some time. By indicating that<br />

time in deadline, rendering requests that occur past the deadline will be ignored.<br />

• failWithDisplayText is a method to tell the share broker that sharing isn’t possible right now,<br />

along with a string that will tell the user why (perhaps the lack of a usable selection). You call this<br />

when you don’t have appropriate data formats or an appropriate selection to share, or if there’s<br />

an error in populating the data package for whatever reason. The text you provide will then be<br />

displayed in the Share charm (and thus should be localized). Scenario 8 of the source app sample<br />

shows the use of this in the simple case when don’t provide data in response to the<br />

datarequested event..<br />

• getDeferral provides for async operations you might need to perform while populating the data<br />

package (just like other deferrals elsewhere in the WinRT API). Do note that datarequested has<br />

a 200ms timout period, after which time the Share charm will display “can’t share right now”.<br />

Requesting a deferral does not change that timeout; it only prevents datarequested from<br />

assuming that the data package is ready once you return from your handler.<br />

The basic structure of a datarequested handler, then, will attempt to populate the minimal<br />

properties of eventArgs.request.data and call eventArgs.request.failWithDisplayText when errors<br />

occur. We see this structure in most of the scenarios in the sample:<br />

var dataTransferManager =<br />

Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();<br />

// Remove this listener as required<br />

dataTransferManager.addEventListener("datarequested", dataRequested);<br />

function dataRequested(e) {<br />

var request = e.request;<br />

// Title is required<br />

496

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

Saved successfully!

Ooh no, something went wrong!