Sharing to Facebook/Twitter from Windows 8/WinRT App

xster
xster
Oct 1, 2012 · 1 min read

Sharing on “Metro style”/”Windows Store” app is a bit different from what we used to. Instead of invoking it directly in code, Microsoft encourages us to use the centralised “charm” bar where we would then implement a page event handler to fill in details to share.

Image for post

To personalise share details, we would fill a DataPackage that would then be used across various media such as email or message or other apps.

To start, you can use

DataTransferManager.GetForCurrentView();

to get the current page’s DataTransferManager. The instance has an event called DataRequested that you can respond to to fill in details whenever the user chooses to share something via the charms bar.

So on OnNavigatedTo of the page, you can do

dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += new TypedEventHandler(dataTransferManager_DataRequested);

and declare

private void dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
DataPackage requestData = e.Request.Data;
requestData.Properties.Title = something;
requestData.SetText("something");
requestData.Properties.Thumbnail = RandomAccessStreamReference.CreateFromUri(new Uri(someURL));
if (!string.IsNullOrEmpty(article.URL)) requestData.SetUri(new Uri(someOtherURL));
}

There’s of course more stuff you can set by referring to http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.datatransfer.datapackage

To get the charm bar share view to show up programmatically, you can use

DataTransferManager.ShowShareUI();

xster

Collection of tech tips and learnings

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store