Derik Whittaker



Lighting up your C# Metro apps by being a Share Target

***** NOTE: This post is valid for the Developer Preview of WinRT and MAY change in later releases *****

Window 8 has enabled developers to hook into various system ‘Contracts’ such as Search, Sharing and Settings. Contracts are like agreements between Windows and one or more Metro style apps, called contract participants, to support some kind of user interaction. There is a contract for each type of interaction, like playing music from an application to a connected stereo, and the contract specifies the types of capabilities the participant has.

In this post we will take a look at how to setup your WinRT/Metro application to be a Share Target.  This means that you are allowing your applications to consume data from other applications, this is pretty cool.

Step 1: Declare Your app as a Share Target

The first thing you need to do is double click on the Package.appxmanifest file in your project.  This should open up the UI component to this file.  Once this is open click on the Declarations tab and choose Share Target from the Available Declarations List as below:


Sub-Step 1a: Determine what data formats and types of files you want to accept

After you have enabled the application to be a Share Target the next thing you need to do is to determine the file types and data types to allow.  To do this you can provide data as below:


Step 2: Listen for the ShareTargetActivated event

Once you have enabled sharing in the Package.appxmanifest it is time to open up your App.Xaml.cs class and override the OnShareTargetActivated method.  It is this method that will be invoked by the OS when your application is selected to receive the data being shared from the Share Source.  Below is my code for doing this.

protected override void OnSharingTargetActivated(ShareTargetActivatedEventArgs args)
    var landingPage = new ShareLandingPage();
    var vm = (landingPage.DataContext as ShareLandingPageViewModel);
    if (vm != null)

        Window.Current.Content = landingPage;

In the code above you can see I am doing a few things.  First I am creating an instance to my ShareLandingPage, this is the view which will be presented to the user during the share process.  I am then calling the SetupShare() method on the attached ViewModel of my ShareLandingPage.  Calling SetupShare will allow me to move responsibility of processing the request off to the correct ViewModel.  Finally I am activating the ShareLandingPage as the current view in the application.

Step 3: Create code to actually accept the shared content

Once you have the logic to handle the ShareTargetActivated.  It is in this method (or set of methods) that your application will check for which type of data is being shared as well as how to handle this request.

In my opinion this logic should not be located in the app.xaml.cs (basically inside the actual event) but rather in some sort of ViewModel or some other helper class.  In my WinRT code I have placed this inside the ViewModel of the view which handles the sharing of content.  The signature to my method is:

public void SetupShare(Windows.ApplicationModel.Activation.ShareTargetActivatedEventArgs args)

Sub-Step 3a: Report to the Share Source that Sharing has started

When you start the process of trying to retrieve the data which is being shared it is a good practice to notify the Share Source that this is happening.  To do this you will want to call the following method on the ShareOperation:


Sub-Step 3b: Inspect to see what type of data/files are being shared

When it comes time to determine what type of content you are being sent you will want to ‘ask’ the DataPackage if it contains a given type.  This can be done by the following code.  However, I have also created an extension class which I feel makes querying for that type cleaner and simpler.

Out of the box code (StandardDataFormats on MSDN)

bool isText = false;
args.ShareOperation.Data.Contains(StandardDataFormats.Text, out isText);

My Extension Methods

// Using the Extension Method
IsTextRequest = args.ShareOperation.Data.IsTextMessage();

// Source for Extension Method
using Windows.ApplicationModel.DataTransfer;
public static class DataPackageExtensions
    public static bool IsTextMessage(this DataPackage dataPackage)
        bool isText = false;
        dataPackage.Contains(StandardDataFormats.Text, out isText);

        return isText;

    public static bool IsUrlMessage(this DataPackage dataPackage)
        bool isUrl = false;
        dataPackage.Contains(StandardDataFormats.Uri, out isUrl);

        return isUrl;

    public static bool IsImageMessage(this DataPackage dataPackage)
        bool isImage = false;
        dataPackage.Contains(StandardDataFormats.Bitmap, out isImage);

        return isImage;

    public static bool IsStorageItemsMessage(this DataPackage dataPackage)
        bool isStorageItems = false;
        dataPackage.Contains(StandardDataFormats.StorageItems, out isStorageItems);

        return isStorageItems;

Sub-Step 3c: Retrieve the shared content

Once you have determined what type of data is being shared it is time that you actually get the values.  Getting the values is much cleaner and simpler than checking for which type is being shared.  To get the values you simply need to call the correct ‘type’ method and you will have your values.



Sub-Step 3d: Report to the Share Source you have finished retrieving the shared content

Once we have finished retrieving the data which is being shared it is a good practice to notify the Share Source that this is true.  You can do this by calling two different methods on the ShareOperation instance.

The first method you will want to call is: args.ShareOperation.ReportDataRetrieved()

The second method you will want to call is args.ShareOperation.ReportCompleted()

By calling these 2 methods you will ensure that the Share Source is notified that their sharing operation is completed and they can return back to what ever operation they were doing prior.

As you can see enabling your application to consume data or files from another WinRT/Metro application is pretty straight forward and pretty easy.

Till next time,

Posted 10-29-2011 3:57 PM by Derik Whittaker
Filed under: , ,



Derik Whittaker wrote Lighting up your C# Metro apps by being a Share Source
on 11-10-2011 9:03 PM

**** NOTE: This post is valid for the Developer Preview of WinRT and MAY change in later releases **

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)