Windows 8 and HTML Part 9: Share Contract

9/4/2012 10:41:30 AM

Continuing with Part 8 of building WinRT applications with HTML and JavaScript.  You can find earlier installments at their respective links: Part 1Part 2Part 3- Part 4- Part 5- Part 6- Part 7- Part 8. If you are interested in the source code for the TweetScan application, you can get it off of GitHub.  Make sure you sign up for How to develop a Windows 8 app in 30 days from Generation App.

Get video here.

The Share Contract allows an application to either Share content with other applications on the user’s machine, or accept content from other applications.  Many applications will be both a Share Source and a Share Target.  In this installment, we look at adding Share Source capability to our TweetScan application.

For TweetScan, we want a user to be able to select a particular tweet he/she is interested in, and share it with other applications.  They may want to post the tweet to Facebook, retweet to other Twitter users, or maybe send it to a friend in an email message.  We will look at added Share Target capabilities to TweetScan in a future installment.  For now, we will just handle turning TweetScan into a Share Source.

To get started, we need to acquire the DataTransferManager.  The DTM is the communication broker between the Share Source and Share target applications.  When the Share Charm is invoked, the DTM will ask the Share Source application for any data that is can share at that time.  If there is data to be shared, the DTM will then look at the system’s installed applications and see which ones can support the type of data being shared (text, html, images, etc.).  You can share more than one type of data in a single share payload.  In fact, you are encouraged to support as many data types as possible so your user gets the broadest choice of Target applications.  It also allows the Target application to provide the best experience it can (html is better looking than text, for example).

Hooking into the DTM is straightforward:

var dtm = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
dtm.ondatarequested = function (e) { ... );

In the ondatarequested function, you set up the data you want to share.  In our case, we want to check the listView being used to display tweets, see if a tweet has been selected, and if so, share that tweet.

dtm.ondatarequested = function (e) {
                var deferral = e.request.getDeferral();
                    function (items) {
                        if (items.length > 0) {
                   = "TweetScan";
                   = "Share tweet from " + items[0].data.from_user_name;
                    function (error) { console.error(error.message); }

A few things to point out. First, getting the selected items for the listView (gridView1) requires the use of a Promise.  Because we are doing an async operation, it is recommended that you get a deferral from the DTM request by calling getDeferral() on the request object.  This lets the DTM know that we may take a little while to finish our processing of the request, and that we will let the DTM know when we are finished with a call to deferral.compete().

It is also important to set the title and description values on the data payload.  This is what is used by the Share Charm UI to let the user know what your application is sharing, and it also used by the Source application to update its UI.

In our example, we are only calling setText() and passing back a simple string representation of the tweet.  As mentioned early, you are encouraged to make additional calls to other set operations (setImage, setHtml, etc.) to provide a richer experience for your users.

All in all, an easy way to get data out of your application and into other ones.


HTML | Windows 8 | WinJS


Comments are closed

Powered by BlogEngine.NET
Theme by Mads Kristensen

About the author

Jeff Brand Jeff Brand

This is the personal web site of Jeff Brand, self-proclaimed .NET Sex Symbol and All-Around Good guy. Content from my presentations, blog, and links to other useful .NET information can all be found here.

E-mail me Send mail


<<  August 2014  >>

View posts in large calendar

My Twitter Updates


Recent comments


The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2014

Sign in