Derik Whittaker

Syndication

News


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

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

This is one of many posts in how to use Contracts within WinRT.  The other posts are:

If you have been paying attention to any of the Windows 8/Metro stuff that has come out sense BUILD you may have noticed that with Windows 8/Metro application developers can now easily light up their applications by enabling their apps to hook into the various system Contracts. These contracts include things like search, settings, file picker and app selection.

In this post we will walk through what is needed for your application to share its content with other applications.  This is known as being the Share Source and is pretty easy to setup.

Step 1: Setup the DataTransfer for the current view

Like the other Contracts in WinRT the Share Source contract is 'context sensitive’ meaning that you can have some pages share data and other pages not, it is completely up to you.  If you want a given page to be a Share Source you will need to do the following

private void SetupDataTransfer()
{
    var dtm = DataTransferManager.GetForCurrentView();

    if (dtm != null)
    {
        dtm.DataRequested += OnDataRequested;
    }
}

The DataTransferManager is in the using Windows.ApplicationModel.DataTransfer namespace.  Of course you will want to call the above code in your pages constructor or in some other path which allows it to wire up the DataRequested event.

Step 2: Handle the DataTransfer Request method

Once you have the code above setup you will need to handle the event once the user hits the Share Charm on the charm bar (not sure if that is the term or not but it is what I call it).  Handling the event is pretty straight forward and can be done directly in the code behind.  However, I really hate doing anything in the code behind so I would rather hand off processing to some View Model.  The code below shows how to handle the request and hand it off.  It also shows how to display some sort of failure message is needed.

void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{

    if (DataContext != null && DataContext is MainPageViewModel)
    {
        ((MainPageViewModel)DataContext).ShareSelectedItem(args.Request);
    }
    else
    {
        args.Request.FailWithDisplayText("Unable to share content at this time...");
    }
}

Step 3: Push the data to be shared to the Share Target

Now that we have actually handled our event and passed it off to the View Model it is time to do something important with it

public void ShareSelectedItem(DataRequest dataRequest)
{

    dataRequest.Data.Properties.Title = "Learning To Share";

    switch (SharingItem)
    {
        case SharingItem.Unknown:
            dataRequest.FailWithDisplayText("No Share Value has been Provided");
            break;
        case SharingItem.String:
            dataRequest.Data.Properties.Description = "Sharing a plain String value";
            dataRequest.Data.SetText(SelectedStringToShare);
            break;
        case SharingItem.Url:
            dataRequest.Data.Properties.Description = "Sharing a Url value";
            dataRequest.Data.SetUri(new Uri(SelectedUrlToShare));
            break;
        case SharingItem.Image:
            dataRequest.Data.Properties.Description = "Sharing a Image Value";
            dataRequest.Data.SetBitmap(ImageStreamReference);
            break;
        default:
            break;
    }

Looking at the code above you can see I am doing a few things.  The first thing I am doing is setting the Title value for the Share request. By setting this title I can give the Share Target.  Once I have set the Title you will notice I have a switch statement, this switch statement is purely to allow the demo app know what data to share.  The important part is inside each case.  Inside the case statements I am setting another piece of Meta Data, the Description.  This again is useful as it gives the Share Target more context as to what is being shared.

Inside the various case statements you can see how we share Text, Uri’s and Bitmaps.

As you can see setting up your application to be a Share Source is pretty easy and really requires very little code.  Hope this helps.

Till next time,


Posted 11-10-2011 8:01 PM by Derik Whittaker
Filed under: , ,

[Advertisement]

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)