How to share text (XAML)
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:
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:
- Windows.Storage. Needed for working with StorageFile and other objects.
- Windows.Storage.Pickers. Used to open the file picker so users can select images and files.
- Windows.Storage.Streams. Often used when sharing images, files, and custom-formatted data.
- Windows.Graphics.Imaging. Useful if you need to modify images before sharing them.
Step 2: Get the DataTransferManager object
The DataTransferManager object is the starting point for any sharing operation.
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.
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.
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.";
Step 6: Add the text to the DataPackage
To add text, use the setText method.
Remarks
To download code that demonstrates how to share text, see our code gallery sample.
Complete example
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!"); }
Related topics
- 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