Visually Located

XAML and GIS

Easily share status with your WinRT App (AKA Share Source Charm)

There are two types of way to share use the Share Charm win Windows8. Your app can be a Share Source, or a Share Target. Think of Share Target as your email or Twitter/Facebook apps. Share Source is everything else. I was  trying to add in the ability to share status within an app and thought it would be a pretty easy thing to do. It was easy, but took me awhile searching the web to find out how to do it.  I’m hoping to make that a little easier for you.

The first thing is to tell the app that users have the ability to share from your app. I thought this was going to be hidden in the app manifest file but was surprised to discover that this is done in code. To register your app as one that can share information, you must subscribe to the DataRequested event from the DataTransferManager.

var dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += DataTransferManagerOnDataRequested; 

I found one example online that said to put that into the constructor of your “MainPage” or first page of the app. The problem with this is that the constructor for pages gets called when navigating backward as well as when going forward. When you navigate backward, the constructor will be called and will register for the event again you get an InvalidOperationException that says “A method was called at an unexpected time.” I’m guessing you get this error because you have already registered for the event with the DataTransferManager and cannot do it again. This is just a wild guess. Instead of wrapping this call with a try/catch in the constructor of your page, you can register for the event within your app.xaml.cs file. You’ll want to do this when the root frame is loaded.

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    ...
 
    var rootFrame = new Frame();
    rootFrame.Loaded += OnRootFrameLoaded;
    
    ...
}
 
private void OnRootFrameLoaded(object sender, RoutedEventArgs routedEventArgs)
{
    var dataTransferManager = DataTransferManager.GetForCurrentView();
    dataTransferManager.DataRequested += DataTransferManagerOnDataRequested; 
}

The advantage of registering for the event within the page is that you can specify which pages in your app can share. If you don’t want your second page to share, you can unsubscribe from the DataRequested event when the user navigates away from the page.

Next you need to decide what to share from your app. This is done from your DataRequested event handler. This seems to be the most cryptic part of sharing. There are so many options to set, but there is not much out there to help you know which ones to set.

You specify your sharing with the DataPackage object. the DataPackage has a few ‘SetXXX’ methods for settings various properties and a Properties property of type DataPackagePropertySet. In order for your app to share, you must set the Title of the DataPackagePropertySet and at least one of the ‘SetXXX’ methods. If you do not, you’ll see the following message when trying to share “There was a problem with the data from <Your App>.”

private void DataTransferManagerOnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    DataPackage package = args.Request.Data;
    package.Properties.Title = "Share Text Example";
    package.SetText("This is what I'm sharing");
}

Each Share Target app is different and expects different properties/methods to be set. I tested with MetroTwit, Rowi, and Tweetro as well as the standard Windows8 apps. I was able to share with MetroTwit, Rowi and the Mail app by only using the SetText method. The mail app would take the Title and use that as the subject. Both MetroTwit and Rowi used the text within the SetText method as the tweet. Tweetro seems to be lacking with sharing. I had to use the SetUri method to enable sharing with it, and it still would not set the tweet text. Using the SetUri method also enabled sharing with the People app. Rowi seemed to be the best Twitter target app. It would use both the Text (SetText) and Uri (SetUri) in the tweet.

Play around with the different ‘SetXXX’ methods to see what other apps allow you to share. One thing I found through testing was that the Description of the DataPackagePropertySet is only used as a nice summary for the user.

blog comments powered by Disqus