Share Text in Between Windows Store Application Using Share Contract

Two Metro applications can share content between them using Share Charm. Content can be as simple as text or may be an image or a file. In content sharing, the application that shares content is called the Source Application and the application that receives content is called the Target Application. The Source Application and the Target Application need to declare the type of content; their intent to share is put in a Declaration.

The Declaration can be configured in the package.appmanifest file. Click on the package.appmanifest file and then select the Declaration tab. From the Available Declarations drop down select Share target and check SupportedAnyFile Type in Supported File Types. See the image below.

ShrMtr1.jpg

Source Application

After configuring the declaration we need to write the code to share relevant text. Let us usea button and in the click event of that button we will share the text.

ShrMtr2.jpg

In code behind attach a click event to the button and call the following function:

ShrMtr3.jpg

The function to share text is as follows. The user can share the text from the Share Charm. Note that in the code below, we have explicitly displayed the Share charm by calling the function showShareUI.

function ShowandShareContract() {

       

        Windows.ApplicationModel.DataTransfer.DataTransferManager.showShareUI();

        var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();

        dataTransferManager.addEventListener("datarequested", function (e) {

            var request = e.request;           

            request.data.properties.title = "Debugmode";

            request.data.properties.description = "Debugmode Share App";

            request.data.setText("Hello from DebugMode App");

        });

    }


The actual data sharing happens in the datarequested event handler. We are sharing the text "Hello from DebugMode App".

Target Application

Now let us create an application, which can consume the shared text. An application can be launched in multiple ways; we need to first check whether the application was launched as Share Target or not before accepting any data.

ShrMtr4.jpg

We also need to check whether the shared operation is containing text or not. That can be checkes as in the following:

ShrMtr5.jpg

The following is the code required to read the text shared and display in an output div:

if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.shareTarget) {

         

           var shareOperation = args.detail.shareOperation;

            if (shareOperation.data.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.text)) {

                var SharedText = " Title :  " + shareOperation.data.properties.title;

                SharedText = SharedText + "  Description : " + shareOperation.data.properties.description;               

                document.getElementById("outputDiv").innerText = SharedText;

               

            }

 

        }

In this way text can be shared between source application and target application using shared contracts. When you run the source application, you will get a ShareContent button.
 
When the Share Charm button is clicked Share Charm will be displayed. You will get a list of all applications that can act as the Target Application. For example the Mail application can also act as a Target Application.

ShrMtr6.jpg

Let us choose the target application we created. Click on the ShareContreactTarget application. You can see two applications can run side by side and text from the source application is displayed in the output div of the target application.

ShrMtr7.jpg

In this way we can share text between two Metro applications. I hope you find this article useful. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all