Sharing Content in Windows Store App Using C#

Today we learn how to share content in Windows 8 Apps using C#. I will create an application for sharing any content via email.

A Sharing feature in an application allows the user to connect and share content with their friends and family and update that conent. The application that enables the user to share content is a best practice and encourages more people to use the application as it connects the user over the internet.

The Sharing feature in Windows 8 Apps enables the user to share content such as text, images, links, files etc. that helps the user to connect with the people. The user can share content over Facebook, SkyDrive, Email etc. thereby helping him to keep updated.

Now, let's start to create a Windows 8 application that helps the user to share content and connect with people though the application.

Step 1

Create a Blank Application using C#.

Step 2

Add the followiing namespaces to your application:

using Windows.ApplicationModel;

using Windows.ApplicationModel.DataTransfer;

Step 3

To enable sharing in the application, I will get the instance of the DataTransferManager class. This class is used to assign the current window to sharing.

DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();

Step 4

After getting the DataTransferManager obect we need to add an event handler of this class that supports sharing. This event is known as the DataRequested event which is fired when the user opens the share window. You need to set this code in your initializing function.

dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager,

                DataRequestedEventArgs>(this.ShareTextHandler);

Step 5

In the event handler declared above, you need to get a DataRequest object and add some properties like title and description to be displayed when the share charm is opened.

DataRequest request = e.Request;

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

            request.Data.Properties.Description = "A demonstration that shows how to share text.";

Step 6

Now, the important thing here is to set the text or content to be shared and add to the DataPackage Class. To set the text to be shared using a textbox, I will create a simple user interface that contains one textbox, as in:

request.Data.SetText(ShareText.Text);

Step 7

Afer setting up everything for sharing, it's time to open the share charm. For this I created a button that opens the share window; see:

DataTransferManager.ShowShareUI();

In the preceding code I use the ShowShareUI() method to open the share windows programmatically. You can also use a tap or charm option to open the share window.

using System;

using Windows.ApplicationModel;

using Windows.ApplicationModel.DataTransfer;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

namespace SharingContent

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }   

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

            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(ShareText.Text);

        }

        private void Share_Click_1(object sender, RoutedEventArgs e)

        {

            DataTransferManager.ShowShareUI();

        }

    }

}

Step 8

Build and run the application. Click on the button; it opens the share charm.

Sharing-Text-in-Windows8-Apps.jpg

Step 9

You can also edit the text from here. Enter the Email id of the recipient and click on the Send button.

Sharing-Content-in-Windows8-Apps.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all