**** NOTE: This post is valid for the Developer Preview of WinRT and MAY change in later releases ****
This is one of many posts in how to use Contracts within WinRT. The other posts are:
If you have been paying attention to any of the Windows 8/Metro stuff that has come out sense BUILD you may have noticed that with Windows 8/Metro application developers can now easily light up their applications by enabling their apps to hook into the various system Contracts. These contracts include things like search, settings, file picker and app selection.
In this post we will walk through what is needed for your application to share its content with other applications. This is known as being the Share Source and is pretty easy to setup.
Step 1: Setup the DataTransfer for the current view
Like the other Contracts in WinRT the Share Source contract is 'context sensitive’ meaning that you can have some pages share data and other pages not, it is completely up to you. If you want a given page to be a Share Source you will need to do the following
private void SetupDataTransfer()
var dtm = DataTransferManager.GetForCurrentView();
if (dtm != null)
dtm.DataRequested += OnDataRequested;
The DataTransferManager is in the using Windows.ApplicationModel.DataTransfer namespace. Of course you will want to call the above code in your pages constructor or in some other path which allows it to wire up the DataRequested event.
Step 2: Handle the DataTransfer Request method
Once you have the code above setup you will need to handle the event once the user hits the Share Charm on the charm bar (not sure if that is the term or not but it is what I call it). Handling the event is pretty straight forward and can be done directly in the code behind. However, I really hate doing anything in the code behind so I would rather hand off processing to some View Model. The code below shows how to handle the request and hand it off. It also shows how to display some sort of failure message is needed.
void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
if (DataContext != null && DataContext is MainPageViewModel)
args.Request.FailWithDisplayText("Unable to share content at this time...");
Step 3: Push the data to be shared to the Share Target
Now that we have actually handled our event and passed it off to the View Model it is time to do something important with it
public void ShareSelectedItem(DataRequest dataRequest)
dataRequest.Data.Properties.Title = "Learning To Share";
dataRequest.FailWithDisplayText("No Share Value has been Provided");
dataRequest.Data.Properties.Description = "Sharing a plain String value";
dataRequest.Data.Properties.Description = "Sharing a Url value";
dataRequest.Data.Properties.Description = "Sharing a Image Value";
Looking at the code above you can see I am doing a few things. The first thing I am doing is setting the Title value for the Share request. By setting this title I can give the Share Target. Once I have set the Title you will notice I have a switch statement, this switch statement is purely to allow the demo app know what data to share. The important part is inside each case. Inside the case statements I am setting another piece of Meta Data, the Description. This again is useful as it gives the Share Target more context as to what is being shared.
Inside the various case statements you can see how we share Text, Uri’s and Bitmaps.
As you can see setting up your application to be a Share Source is pretty easy and really requires very little code. Hope this helps.
Till next time,
11-10-2011 8:01 PM