Monthly Archives: December 2012

Creating a Sharing Contract for Windows 8 & RT Apps

By Don Burnett
Expression Blend MVP

The coolest new feature that I really love about Windows 8 and Windows RT is that they now have a new universal sharing interface. If you swipe inward on a touch screen from the right side of the screen (or mouse to the top right pixel) you will get presented with a new menu of options called “Charms” one of these is a sharing button. If you have a new surface or brand new Windows 8 keyboard from Microsoft you will notice this is a button in the new layout as well.


When you select the charms button a new side panel will appear with different apps that will let you share to and from. If you are trying to share from a desktop app I recommend an app on the Windows store called “Clipboard” which will let you take stuff easily through the new sharing interface should your app not be clipboard enabled with the desktop.


Implementing a Sharing Contract in Our App

The first thing we will need to do is to wire up the DataTransferManager into the MainPage of our app code..

Using Windows.ApplicationModel.DataTransfer;

Next we create an event handler for it in code

DataTransferManager manager = DataTransferManager.GetForCurrentView();
manager.DataRequested += manager_DataRequested;

Next we create some simple event handler code so when the share charm is invoked to the user the event is passed to our page..

void manager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
DataRequest request = args.Request;
request.Data.Properties.Title = "Handy Dandy Share contract";
request.Data.Properties.Description = "Share some text";
request.Data.SetText("Sharing is Caring..");

The framework decides on the correct share client based on the format of the content that we plan to share.. The example above is text, but you could easily wrap in a share to Facebook or a share to twitter by simply adding code for it to your app..


If my app were using HTML for instance and I was sharing some HTML info that I wanted to share over mail it might look like this..

void manager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
  DataRequest request = args.Request;
  request.Data.Properties.Title = string.Format("Sharing Is Caring - {0}", CurrentPortTitle);
  request.Data.Properties.Description = "Sharing this info";
  TilePort sharePort = TilesQueue.ToArray().FirstOrDefault(p => p.ID.Equals(CurrentPortID));
  string htmlMail = BuildHtml(CurrentPort);
  string htmlFormat = HtmlFormatHelper.CreateHtmlFormat(htmlMail);

Types of data you are allowed to share from your app..

  • Plain text
  • Uniform Resource Identifiers (URIs)
  • HTML
  • Formatted text
  • Bitmaps
  • Files
  • Developer-defined data

The core of any sharing operation is the DataPackage object. This object contains the data the user wants to share. A DataPackage can contain a delegate. A delegate is a function that is called when the receiving app requests data. You should use a delegate any time that the data a user wants to share is resource-intensive. A delegate can help your app share data more efficiently.

The example above only is a simple text sharing contract..

To share content such as images or files, we will need to add the following namespaces as well. Here’s a list of the namespaces necessary..

  • Windows.Storage. Needed for working with StorageFile and other objects.
  • Windows.Storage.Pickers. Used to open the file picker so users can select images and files.
  • Windows.Storage.Streams. Often used when sharing images, files, and custom-formatted data.
  • Windows.Graphics.Imaging. Useful if you need to modify images before sharing them.

Sharing a hyperlink

private void RegisterForShare()


// create DataTransferManager object

DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();

// create event handler

dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareLinkHandler);


private void ShareLinkHandler(DataTransferManager sender, DataRequestedEventArgs e)


  // Set title and Description Properties

DataRequest request = e.Request; request.Data.Properties.Title = “Share My Cool Link”; request.Data.Properties.Description = “Add a link (URI) to share.”;

// To add the link, use the SetUri method.

request.Data.SetUri(new Uri(;


Share an Image

Here’s an example to share an image..

private void RegisterForShare()

DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareImageHandler);

private async void ShareImageHandler(DataTransferManager sender, DataRequestedEventArgs e)

{ DataRequest request = e.Request; request.Data.Properties.Title = “Share My Graphic Image”; request.Data.Properties.Description = Image Sharing.”;

// Remember also when we start making async calls in the DataRequested event handler,
// we need to get the deferral before anything else..

DataRequestDeferral deferral = request.GetDeferral();

// Make sure we always call Complete on the deferral.

StorageFile thumbnailFile = await Package.Current.InstalledLocation.GetFileAsync(“Assets\\VNEXTLOGO.png”);

request.Data.Properties.Thumbnail = RandomAccessStreamReference.CreateFromFile(thumbnailFile);

StorageFile imageFile = await Package.Current.InstalledLocation.GetFileAsync(“Assets\\VNEXTLOGO.png”);