Home > Metro, Windows 8 > Windows 8 Sharing Contract

Windows 8 Sharing Contract

Windows 8 Apps support a new type of sharing, which is kind of Clipboard 2.0.

Every app can share data to any other app.

The user is in charge of this sharing, because this enables apps to break out of their sandbox. This looks like this:

User selects an image in the photo app (green), then shares it to e.g. the mail app to send the image via mail.


The sharing target app displays a dialog (red), where the user can add some additional data and then close the dialog to go back to the share source app.

Sharing Source App:

In order to use this sharing contract the share source app must implement this:

private void btnShare_Click_1(object sender, RoutedEventArgs e)
    dtMgr = DataTransferManager.GetForCurrentView();
    dtMgr.DataRequested += dtMgr_DataRequested;

void dtMgr_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
    args.Request.Data.Properties.Title = "Data from Sample Share app";
    args.Request.Data.Properties.Description = "Description from Sample Share app";
    args.Request.Data.Properties.Thumbnail = 
                new Uri("ms-appx:///Assets/SharingIcon.png"));

    // Share Text data

    // Share Uri data
    args.Request.Data.SetUri(new Uri(txtUri.Text));


Sharing Target app:

The sharing target app needs a little bit more effort:

First, a new XAML Page of type "Share Target Contract" must be added.

When you add this page, then the wizard will also take care of adding the declaration "Share Target" to the Package.appxmanifest.

If you use any other page, then this must be done manually!

The declaration does also define, which type of data will be accepted for sharing, which can be any of the default formats in Windows.ApplicationModel.DataTransfer.StandardDataFormats, but also any custom data format

The dialog looks like this:


The wizard does also add the overload function OnShareTargetActivated to App.xaml.cs, which must be done manually, if some other XAML page will be added as target page.


/// <summary>
/// Invoked when the application is activated as the target of a sharing operation.
/// </summary>
/// <param name="args">Details about the activation request.</param>
protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
    var shareTargetPage = new ShareTargetPage();



When using any other XAML page as target page, then make sure, to add the statement


to the top of the target page and make sure, that the UI is 645 px in width.

The header will always be displayed by the system and will take about 80 px in height.

Example for a sharing target page:



The sharing target page must include a function called Activate(), which will deliver the data from the source application, after the user has selected the target app.

args.ShareOperation does contain the data from the source app or allows to fetch data from the source app using e.g. Data.GetTextAsync(), Data.GetUriAsync(),….

After the app is finished with the shared data, it must report back to the source app.

Also this is done on top of the ShareOperation using ReportCompleted()

Optionally the app can return a so called quicklink, which will add a new entry to the share target list and allow the user to select the same target parameters very quickly the next time.

The mail app e.g does remember the last recipient and adds a quicklink to the target selection list.

Example where the list of quick links is marked in red:



/// <summary>
/// Invoked when another application wants to share content through this application.
/// </summary>
/// <param name="args">Activation data used to coordinate the process with Windows.</param>
public async void Activate(ShareTargetActivatedEventArgs args)
    this.shareOperation = args.ShareOperation;

    // Communicate metadata about the shared content through the view model
    var shareData = this.shareOperation.Data;
    var shareDataProperties = shareData.Properties;

    txtStatus.Text = "AppName: " + shareDataProperties.ApplicationName + 
                     "\nTitle: " + shareDataProperties.Title + "\n" +
                     "\nDescription: " + shareDataProperties.Description + 
                     "\nQuicklink: " + shareOperation.QuickLinkId + 
                     "\nAvailable Formats: ";

    for (int i = 0; i < shareData.AvailableFormats.Count; i++)
        txtStatus.Text += "\n - " + shareData.AvailableFormats[i];

    if (this.shareOperation.Data.Contains(StandardDataFormats.Uri))
        txtStatus.Text += "\n\n";
        Uri uri = await this.shareOperation.Data.GetUriAsync();
        if (uri != null)
            txtStatus.Text += "\nUri: " + uri.AbsoluteUri + Environment.NewLine;

    if (this.shareOperation.Data.Contains(StandardDataFormats.Text))
        txtStatus.Text += "\n\n";
        string text = await this.shareOperation.Data.GetTextAsync();
        if (text != null)
            txtStatus.Text += "\nText: " + text + Environment.NewLine;

    if (this.shareOperation.Data.Contains(StandardDataFormats.StorageItems))
        txtStatus.Text += "\n\n";
        IReadOnlyList<IStorageItem> storageItems = null;
        storageItems = await this.shareOperation.Data.GetStorageItemsAsync();
        string fileList = String.Empty;
        for (int index = 0; index < storageItems.Count; index++)
            fileList += storageItems[index].Name;
            if (index < storageItems.Count - 1)
                fileList += ", ";

        txtStatus.Text += "StorageItems: " + fileList + Environment.NewLine;

    if (this.shareOperation.Data.Contains(StandardDataFormats.Html))
        txtStatus.Text += "\n\n";
        string htmlFormat = await this.shareOperation.Data.GetHtmlFormatAsync();
        string htmlFragment = HtmlFormatHelper.GetStaticFragment(htmlFormat);

        txtStatus.Text += "HTML: " + Environment.NewLine;
        this.MyWebView.Visibility = Visibility.Visible;
        this.MyWebView.NavigateToString("<html><body>" + htmlFragment + "</body></html>");
    if (this.shareOperation.Data.Contains(StandardDataFormats.Bitmap))
        txtStatus.Text += "\n\n";
        this.MyIMageLabel.Visibility = Visibility.Visible;
        MyImage.Visibility = Visibility.Visible;
        IRandomAccessStreamReference imageReceived = 
            await this.shareOperation.Data.GetBitmapAsync();
        IRandomAccessStreamWithContentType stream = 
            await imageReceived.OpenReadAsync();

        BitmapImage bitmapImage = new BitmapImage();
        MyImage.Source = bitmapImage;

    Window.Current.Content = this;

private async void btnShare_Click_1(object sender, RoutedEventArgs e)
    QuickLink ql = new QuickLink()
        Id = "HS42",
        Title = "MyQuicklink",
        SupportedFileTypes = { ".txt", ".jpg", ".bmp", ".gif", 
                               ".docx", ".pptx", ".xlsx", ".pdf", 
                               ".wmv", ".mp3", ".mp4", ".wma" },
        SupportedDataFormats = { StandardDataFormats.Text, 
                                 StandardDataFormats.StorageItems }
    StorageFile iconFile = await Windows.ApplicationModel.Package.Current
        .CreateFileAsync("Assets\\Quicklink.png", CreationCollisionOption.OpenIfExists) 
        as StorageFile;
    ql.Thumbnail = RandomAccessStreamReference.CreateFromFile(iconFile);




Categories: Metro, Windows 8
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: