Windows 8 – Adding a Share Contract to your App in C#

I’ve finally started playing around with Windows 8 after spending ages getting it to work in VirtualBox. I struggled to find any sample code / tutorials on adding a share contract in C# so here is my guide.

 

First things first we need to add a import statement:

Using Windows.ApplicationModel.DataTransfer;

 

Next up we need to fetch a DataTransferManager and then associate a method with the DataRequested event. I placed this code in the constructor of the MainPage.xaml.cs.

DataTransferManager manager = DataTransferManager.GetForCurrentView();
manager.DataRequested += new TypedEventHandler(share_DataRequested);

 

Now we need to create the share_DataRequested Method that’s called when a share request is made.

private void share_DataRequested(object sender, DataRequestedEventArgs e)
{
    DataRequest request = e.Request;
    request.Data.Properties.Title = "Sharing something";
    request.Data.Properties.Describtion = "Sharing something with someone";
    request.Data.setText("I'm sharing!");

}

 

The method above is where you tell Windows what data your app wants to share. The example above shows the sharing of a simple string saying “I’m sharing”. The picture below shows how the message will apear once the user select a Share compatible App to share with. On the Enterprise Edition you can only share using Mail by default so you may need to download some other Apps to test the sharing.

 

Your App now makes use of a share contract! If you want to manually launch the Share Contract UI for the user you can use the following code:

DataTransferManager.ShowShareUI();

 

Which will bring up the below UI.

 

Leave a Reply

Your email address will not be published. Required fields are marked *