Partager via


How to share a link (XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Links are another common data format that users want to share. The most common reason to share a link is when a user has specifically selected one; however, we also recommend that your app support sharing links:

  • When the content the user selects is also available online.
  • As a secondary source when sharing HTML.

Note  

Links can also be shared by using Tap and send. Tap and send is sharing through Near Field Communication (NFC).

 

What you need to know

Technologies

Prerequisites

  • You should be familiar with Visual Studio and its templates.
  • You should be familiar with developing in C#/C++.

Instructions

Step 1: Add the DataTransfer namespace

You need to add the right namespaces to your app so you can create and process the objects related to sharing. At a minimum, you should add the Windows.ApplicationModel.DataTransfer namespace:

using Windows.ApplicationModel.DataTransfer;
using namespace Windows::ApplicationModel::DataTransfer;

This namespace has all you need for basic sharing. Remember, though, if you want to share content such as images or files, you'll need to add those namespaces as well. Here's a list of the namespaces you might need:

Step 2: Get the DataTransferManager object

The DataTransferManager object is the starting point for any sharing operation.

DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
DataTransferManager^ dataTransferManager = DataTransferManager::GetForCurrentView();

Step 3: Add an event handler for the DataRequested event

Add a DataRequested event handler to fire when the user wants to invoke Share. In a Windows Store app, this occurs automatically when the user invokes the Share charm. If you're developing for Windows Phone, there is no built-in Share charm, so you'll need to add a control for the user to tap and trigger the handler.

dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, 
    DataRequestedEventArgs>(this.ShareLinkHandler);
auto dataRequestedToken = dataTransferManager->DataRequested += ref new TypedEventHandler<DataTransferManager^, 
    DataRequestedEventArgs^>(this, &MainPage:: ShareLinkHandler);

Step 4: Get a DataRequest object

When a DataRequested event occurs, your app receives a DataRequest object. This object contains a DataPackage that you can use to provide the content that the user wants to share.

DataRequest request = e.Request;
DataRequest^ request = e->Request;

Step 5: Set the title and description properties

The title property is mandatory and must be set.

// The title is mandatory
request.Data.Properties.Title = "Share Link Example";
request.Data.Properties.Description = "Demonstrates how to add a link to share.";
// The title is mandatory
request->Data->Properties->Title = "Share Link Example";
request->Data->Properties->Description = "Demonstrates how to add a link to share.";

To add the link, use the SetWebLink method.

request.Data.SetWebLink(new Uri("http://www.fabrikam.com"));
request->Data->SetWebLink(ref new Uri("http://www.fabrikam.com"));

Complete example

Here's an example of a function that sets a link for a user to share. For a more complete example, check out our code gallery sample.

private void RegisterForShare()
{
    DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
    dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, 
        DataRequestedEventArgs>(this.ShareLinkHandler);
}

private void ShareLinkHandler(DataTransferManager sender, DataRequestedEventArgs e)
{
    DataRequest request = e.Request;
    request.Data.Properties.Title = "Share Link Example";
    request.Data.Properties.Description = "Demonstrates how to add a link to share.";
    request.Data.SetWebLink(new Uri("http://www.fabrikam.com"));
}
void MainPage::RegisterForShare()
{
    DataTransferManager^ dataTransferManager = DataTransferManager::GetForCurrentView();
    auto dataRequestedToken = dataTransferManager->DataRequested += 
        ref new TypedEventHandler<DataTransferManager^, DataRequestedEventArgs^>(
            this, &MainPage::ShareLinkHandler);
}

void MainPage::ShareLinkHandler(DataTransferManager^ sender, DataRequestedEventArgs^ e)
{
    DataRequest^ request = e->Request;
    request->Data->Properties->Title = "Share Link Example";
    request->Data->Properties->Description = "Demonstrates how to add a link to share.";
    request->Data->SetWebLink(ref new Uri("http://www.fabrikam.com"));
}

Sharing content source app sample

Sharing and exchanging data

How to share files

How to share HTML

How to share text

Quickstart: Sharing content

DataPackage

Windows.ApplicationModel.DataTransfer

Windows.ApplicationModel.DataTransfer.Share