Compartir a través de


How to share text (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]

Sharing text is one of the most basic, yet essential, methods for sharing content. In addition to plain-text messages, such as status updates, we recommend that your app support sharing text:

  • When you want the content to be available to a large number of target apps.
  • As a secondary source when sharing links or sharing HTML.

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: Adding the DataTransfer namespace

You need to do 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.ShareTextHandler);
auto dataRequestedToken = dataTransferManager->DataRequested += ref new TypedEventHandler<DataTransferManager^, 
    DataRequestedEventArgs^>(this, &MainPage::ShareTextHandler);

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 Text Example";
request.Data.Properties.Description = "A demonstration that shows how to share text.";
// The title is mandatory
request->Data->Properties->Title = "Share Text Example";
request->Data->Properties->Description = "A demonstration that shows how to share text.";

Step 6: Add the text to the DataPackage

To add text, use the setText method.

request.Data.SetText("Hello World!");
request->Data->SetText("Hello World!");

Remarks

To download code that demonstrates how to share text, see our code gallery sample.

Complete example

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

private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs e)
{
    DataRequest request = e.Request;
    request.Data.Properties.Title = "Share Text Example";
    request.Data.Properties.Description = "A demonstration that shows how to share text.";
    request.Data.SetText("Hello World!");
}
void MainPage::RegisterForShare()
{
    DataTransferManager^ dataTransferManager = DataTransferManager::GetForCurrentView();
    auto dataRequestedToken = dataTransferManager->DataRequested += ref new TypedEventHandler<DataTransferManager^, DataRequestedEventArgs^>(
        this, &MainPage::ShareTextHandler);
}

void MainPage::ShareTextHandler(DataTransferManager^ sender, DataRequestedEventArgs^ e)
{
    DataRequest^ request = e->Request;
    request->Data->Properties->Title = "Share Text Example";
    request->Data->Properties->Description = "A demonstration that shows how to share text.";
    request->Data->SetText("Hello World!");
}

Sharing content source app sample

Sharing and exchanging data

How to share files

How to share HTML

How to share a link

Quickstart: Sharing content

DataPackage

Windows.ApplicationModel.DataTransfer

Windows.ApplicationModel.DataTransfer.Share