Adding Share Charm in Windows 8 application

I recently developed Ghazals of Ghalib app for Win 8 store. Now I am applying an update to it.The task is simple, adding a share charm or adding the facility of sharing an app’s data through email or facebook. I hope before reading this article you have the basic knowledge of creating windows 8 apps. But in case you don’t, you may want to have a look at this article (Win 8 development).

So keeping in mind that you have some basic knowledge about creating windows 8 app, let us start a quick guide for adding the share charm.

Step 1: Enabling the internet for sharing

Just open the manifest file and check the internet(client) in the capabilities.

internet

Step 2: Adding the libraries

We will have to import some of the required libraries. The libraries and the coding will be done in the “.cs” file of the page from which data is to be shared.

Using Ghazals_of_Ghalib.Data // This is the Data Library of the project you are adding share charm in, imported for accessing the app data//
Using Windows.ApplicationModel.DataTransfer // For transferring data over the internet//
Using Windows.Storage.Sreams // For carrying the data to the internet//

internet2

Step 3: Getting dirty with the coding

All we have to do now is to get dirty with the coding. Lets us start by adding a simple line of code in the LoadState method:

DataTransferManager.GetForCurrentView().DataRequested += OnDataRequested; //this line of code  registers all the data request events.//

Now all we have to do is to handle the data request event. So lets create a method:

void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{

}

Now we have to get the data from the current page and assign it to the args parameter of the method


var request = args.Request;
var item = (RecipeDataItem)this.flipView.SelectedItem;
request.Data.Properties.Title = item.Title;
request.Data.Properties.Description = "How to Read and Explaination";

Then share it:

var sharetext = “\r\nHow to read\r\n”;
sharetext += String.Join(“\r\n”, item.howtoread);
sharetext+= (“\r\n\r\nExplaination\r\n” + item.explaination);
request.Data.SetText(sharetext);

at last share the ghazals image:


var reference = RandomAccessStreamReference.CreateFromUri(new Uri(item.ImagePath.AbsoluteUri));
request.Data.Properties.Thumbnail = reference;
request.Data.SetBitmap(reference);

Internet 3

 

That’s all Folks. Just run the app and enjoy. I hope this article was helpful. Any suggestions would be highly appreciated.