Everybody loves to share. (At least after they are beaten into doing so in pre-school.)
The ability to share from one application to another can be anything from useful to critical. Historically, we’ve seen a number of ways of doing this, the most enduring of which is the clipboard. Windows 8, however, introduces a system-wide sharing mechanism that is like the clipboard on steroids, and more important, which is easy to program and easy to use.
Sharing will be critical to our Conference Buddy application as we’ll want to share contacts and event information.
Architecture
The process of sharing sounds complicated, but the programmer’s role is pretty straightforward, and made even easier by the sharing target contract that we’ll talk about in the next posting.
Here’s the architecture:
One application is the source. This is the provider of information and the part we’ll implement today. The source registers with the DataTransferManager and when the user wants to share the source receives an event which it responds to by filling a DataPackage.
The (invisible, behind-the-scenes) ShareBroker filters the list of Target Apps (which we’ll build tomorrow) that can handle the types of data you are sharing. The user selects the target app of choice and that application is activated. The targret processes the data package contents and reports complete.
Implementation
Creating a Sharing source is incredibly easy. Create a new application and call it ShareSource. Give the view a simple TextBlock that says “hello.”
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <TextBlock FontSize="20" Text="Welcome to Sharing!" /> </Grid>
Turn to OnNavigatedTo. Here we create an instance of the DataTransferManager by calling GetForCurrentView, a static method on the DataTransferManager class. Once we have that, we can register for the DataRequested event handler.
protected override void OnNavigatedTo(NavigationEventArgs e) { DataTransferManager dtm = DataTransferManager.GetForCurrentView(); dtm.DataRequested += dtm_DataRequested; }
In the event handler we set the Title and the description through the args parameter,
void dtm_DataRequested( DataTransferManager sender, DataRequestedEventArgs args ) { args.Request.Data.Properties.Title = "My data from my application"; args.Request.Data.Properties.Description = "Description of my data from my application";
We then set the data, using any number of data types. For example, to set text, we would write,
args.Request.Data.SetText( "This is text from my source" );
Similarly you can set HTML, but HTML is slightly tricky as a special format is expected. You simplify this problem by using the static CreateHtmlFormat method of the HtmlFormatHelper class,
args.Request.Data.SetHtmlFormat( HtmlFormatHelper.CreateHtmlFormat( "<b>Important</b> data from my application placed in <i>HTML</i>" ) );
That’s it! You can now run your application and when it starts, swipe to bring in the charms. Click on Search and you will be presented with a list of all the applications registered on your computer that can handle the data types you are presenting (text and html), as shown in the cropped image at the top of this posting.
Touch on Mail and notice that the Title you entered is used as the subject, and the HTML you entered is used as the body of the message.
If the target you chose only supported plain text, then the text message would have been used instead of the HTML.