How to receive text (XAML)
To receive shared text, your app needs to support the Share contract, handle share activation events, and use the ShareOperation object to retrieve the shared text content.
Text is perhaps the most common of all data formats that users share. In addition to being the first choice for content such as status updates, it is a recommended option for apps that share links, HTML, and other information.
When writing a share target app, you should support text as a part of sharing because it ensures that users can select your app in a wide variety of situations. That said, do not support text if you think it will only confuse users. For example, if your app deals mostly with files, a text representation of those files isn't going to meet user expectations.
What you need to know
Technologies
Prerequisites
- You should be familiar with Visual Studio and its associated templates.
- You should be familiar with C#/C++ development.
Instructions
Step 1: Support the Share contract
Before your app can receive shared content, you have to declare that it supports the Share contract. This contract lets the system know that your app is available to receive content. If you're using a Visual Studio template to create your app, here's how you support the Share contract:
- Open the manifest file. It should be called something like package.appxmanifest.
- Open the Declarations tab.
- Choose Share Target from the Available Declarations list.
- Click Add to add support for the Share Target contract in your app.
Step 2: Specify that your app supports text
To specify that your app supports text as a data format:
- Open the manifest file.
- In the Data Formats section, click Add New.
- Type text. Don't include the period (.).
The preceding steps add the following section to the manifest.
<Extensions> <Extension Category="windows.shareTarget"> <ShareTarget> <DataFormat>text</DataFormat> </ShareTarget> </Extension> </Extensions>
Step 3: Adding the necessary namespaces
For a target app, you'll need the Windows.ApplicationModel.Activation, Windows.ApplicationModel.DataTransfer, and Windows.ApplicationModel.DataTransfer.ShareTarget namespaces:
using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.DataTransfer; using Windows.ApplicationModel.DataTransfer.ShareTarget;
Step 4: Handling share activation
When a user selects your app to share content, the system activates your app by calling the app's Application.OnShareTargetActivated method. You need to override this method to get the content that a user wants to share.
void App::OnShareTargetActivated(ShareTargetActivatedEventArgs^ args) { // Code to handle activation goes here. }
Step 5: Get the ShareOperation object.
The ShareOperation object contains all the data that your app needs, to get the content that a user wants to share.
Step 6: Defer processing of share data.
The Application.OnShareTargetActivated method must return quickly. After retrieving the ShareOperation object, return from the Application.OnShareTargetActivated method and asynchronously complete the share data processing.
Step 7: Check to see if the share data contains text.
The ShareOperation object contains a DataPackageView object. This object is essentially a read-only version of the DataPackage object that the source app used to create the data. Use this object to see if the content being shared is available in text format.
if (shareOperation->Data->Contains(StandardDataFormats::Text)) { // Code to process text goes here. }
Even if your app supports only text, you should add a statement similar to the preceding code. It makes it easier to support other data types and file formats later, if you choose to do this.
Step 8: Process the text.
To get the text, call the DataPackageView.GetTextAsync method.
create_task(shareOperation->Data->GetTextAsync()).then([this](String^ text)
{
sharedText = text;
});
Of course, what you do with the text depends on your app.
Step 9: Call ReportCompleted.
After your app finishes processing the content that the user wants to share, call ReportCompleted so the system knows your app is no longer needed.
Remarks
For more detailed information on how apps can receive text, check out our code sample.
Related topics
- Sharing content target app sample
- Sharing and exchanging data
- How to receive HTML
- How to receive a link
- Quickstart: Receiving shared content
- DataPackage
- Windows.ApplicationModel.DataTransfer
- Windows.ApplicationModel.DataTransfer.Share
- Guidelines for debugging target apps