Share Links or URIs in Windows Store App Using C#

In my previous article I showed you how to share some text in Windows 8 Apps. Today I will teach another data format that you can share with your friends. 

In this article I will show you another data format that is supported by DataTransferManager. You can share Links or URIs in Windows 8 Apps. Sharing Links and URIs is important when you want to recommend links to your friends. Suppose you see a list of articles in an application and you want to share the links or URIs of selected articles with your friend; to do this your application must support sharing links.

Here I extend my previous article where I retrieve a RSS data Feed of articles in Windows 8 Apps. I am adding a sharing feature to this application, so that the user is also able to share the link of a selected article from the list using the sharing charm of the application.

Now, let's begin to add this feature.

Step 1

Here we add the namespace needed for the application to support the sharing feature; see:

using Windows.ApplicationModel.DataTransfer;

Step 2

In this step we get the view of the sharing charm for the sharing operation using the DataTransferManager Class.

DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();

Step 3

Then, We associate an event handler of DataTransferManager Class for DataRequested Event.

dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager,

                DataRequestedEventArgs>(this.ShareLinkHandler);

The above event is fired when the user opens the Share Charm. This event handler must be set in some Initialization event to an application such as OnNavigateTo event of Windows 8 Apps.

Step 4

In the event we get the DataRequest Obejct and set some properties for sharing operations, as in:

DataRequest request = e.Request;

request.Data.Properties.Title = "Share Link Example";

request.Data.Properties.Description = "Demonstrates how to add a link (URI) to share.";

Step 5

Here we set the Link which we want to share in the DataRequested event, as in:

FeedItem f = new FeedItem();

f.Link = feed.Items[ItemListView.SelectedIndex].Links[0].Uri;        

request.Data.SetUri(f.Link);

In the preceding code we set the URI of the DataPackage after the user selects an article from the listview and opens the share charm to share.

Step 6

Here is the complete code for the share Link or URI from the Windows 8 Apps:

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 (URI) to share.";

    FeedItem f = new FeedItem();

    f.Link = feed.Items[ItemListView.SelectedIndex].Links[0].Uri;        

    request.Data.SetUri(f.Link);

}

Step 7


Build and Run the application to see the output. Select any Article and open the Share Charm.

Sharing-links-In-Windows8-Apps.jpg

Step 8

You will see that your app is now ready to share the link of a selected article.

Sharing-content-in-Windows8-apps.jpg


Similar Articles