wp7appdevelopment

Adventures in Windows Phone app development

Monthly Archives: May 2013

Integrating with the Sharing Charm (Windows 8/Win8 Metro)

Simple example of integrating with the Sharing charm in a Windows 8 (Win8 metro) app. This example assumes XAML/C#, and shows how to share plain text.  It’s also possible to share URIs, HTML, formatted text, bitmaps, files, and developer-defined data.

  • The first thing you need to do is add the DataTransfer namespace. This is all that’s needed for sharing text.
using Windows.ApplicationModel.DataTransfer;
  • Next, get the instance of the DataTransferManager class that’s been assigned to the current window:
DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
  • This class supports a datarequested event, which is fired when the user taps the Share charm. You will need to listen for this event to know when the user wants to share something. The following registers an event handler that is called whenever a datarequested event occurs. This handler gets a DataRequestedEventArgs object, which contains the DataRequest object.
dataTransferManager.DataRequested +=
    new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareTextHandler);
  • Next, in the data handler (ShareTextHandler in this example), set the data that the user wants to share:
private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs e)
{
    DataRequest request = e.Request;

    // Now add the data you want to share.
    request.Data.Properties.Title = "Share Text Example";
    request.Data.Properties.Description = "A demonstration that shows how to share text.";
    request.Data.SetText("Include your share data here. This will be used by applications (such as email or twitter)");
}

Complete Example:

using Windows.ApplicationModel.DataTransfer;
//To see this code in action, add a call to RegisterForShare to your constructor or other
//initializing function.
private void RegisterForShare()
{
    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("Hello World!");
}

Known bugs:

This approach works fine if you have a single page. But if you navigate to a different page and then navigate back, you’ll get an error when you try to add the handler a second time. The work-around for this is to remove the handler in your OnNavigatingFrom method:


protected override void OnNavigatingFrom(NavigationEventArgs e)
{
DataTransferManager datatransferManager = DataTransferManager.GetForCurrentView();
datatransferManager.DataRequested -= DataRequested;
}

Alternate Approach: Create ShareablePage abstract class

In my app, I had multiple pages. And I wanted the user to be able to share a link to my app from any of these pages. To avoid duplicating the code and making sure I added/removed the handlers each time I navigated to/from a new page, I created a ShareablePage abstract class.

My ShareablePage class has OnNavigatedTo and OnNavigatedFrom methods, which take care of registering and removing the handlers for the Sharing Charm datarequested.

I modified LayoutAwarePage to inherit from my ShareablePage class. (I also needed to modify the OnNavigatedTo/OnNavigatedFrom methods in LayoutAwarePage to call base functions.) Now when I create a new LayoutAwarePage in my app, it automatically is integrated with the Sharing charm.

using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace MyApp
{
    public abstract class ShareablePage : Page
    {
        private DataTransferManager dataTransferManager;
        private TypedEventHandler<DataTransferManager, DataRequestedEventArgs> shareHandler;

        public ShareablePage()
        {
            dataTransferManager = DataTransferManager.GetForCurrentView();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            shareHandler = new TypedEventHandler<DataTransferManager,
                       DataRequestedEventArgs>
                       ((s, args) =>
                       {
                           Share(args.Request);
                       });
            dataTransferManager.DataRequested += shareHandler;
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            dataTransferManager.DataRequested -= shareHandler;
            base.OnNavigatedFrom(e);
        }

        protected void Share(DataRequest dataRequest)
        {
            // Now add the data you want to share.
            dataRequest.Data.Properties.Title = "My App";
            dataRequest.Data.Properties.Description = "Check this out!";
            dataRequest.Data.SetText("... include info to share....");
        }
    }
}

Key steps:

  1. Add ShareablePage class to your project
  2. Modify LayoutAwarePage class:
    • Inherit from ShareablePage class
    • OnNavigatedTo method needs to call base.OnNavigatedTo()
    • OnNavigatedFrom method needs to call base.OnNavigatedFrom()
  3. Create new LayoutAwarePages as usual.

Resources:

For more information on sharing other types of information, like images or files, see “Quickstart: Sharing content (Windows Store apps using C#/VB/C++ and XAML)”