Freigeben über


Integrating With Windows 8 Share Charm - Part 1: Sharing Data

                                                                                                           Windows 8 Share Charm – Sharing Data

This blog post describes the process of implementing sharing in Windows 8. Windows 8 share charms lets the users share content from within the app with other people or apps, and receive shared content. Below is a snapshot of the Images App that I will be using to demonstrate. The app uses “Bing Image Search” API to search for images based on the keyword provided by the user.

There are 2 parts to sharing content. One is to be able to share content with other apps and the other is the ability to receive content from other applications. I will implement the sharing logic with in the Bing Images app, and will create another target app to demonstrate how to receive the shared content. In this blog post I will only discuss the, Sharing part of the application.

When the user selects an image, it is displayed in its full view as shown below.

 

Within this view, I want to enable the user, to share this image with other apps.

There is a static class called DataTransferManager that allows apps to initiate sharing operations. Within the constructor of the full view page, get the reference to the DataTransferManager using the static method GetForCurrentView and register for the DataRequested event.

Pitfalls:

  1. The method DataTransferManager.GetForCurrentView returns the reference to the DTM of the Window, not the page that is currently loaded within the Window. This will result in a situation, where sharing can be initiated from within any page. For instance once the user opens the image in FullImageView (for the first time) and then goes back to Main page, he will still be able to initiate sharing (from the Main page) even though our intention was to allow sharing only in the FullImageView.
  2. The second pitfall is that every time the user navigated to the FullImageView the constructor gets called and within the constructor we are registering to the DataRequested event. Everything is good when the user navigates to the FullImageView, but every subsequent navigation to the FullImageView will result in an exception, as shown below.

Solution:

One way to solve these problems is to unregister the DataRequested event, when we navigate away from the FullImageView by overriding the FullImageView’s OnNavigatedFrom method as shown below.

The DataRequested event that we have registered to is called whenever the Share contract is selected. Within the event handler we need to write logic to share the data. The DataRequestedEventArgs object passed to the event handler has the property called Request. The request property contains a Data property of type DataPackage that can be used to share data with other Apps. The DataPackage can contain various types Data including Text, URI, Bitmap, HtmlText etc. These can be set using the DataPackage classes “Set” methods.

For this example, we will set only the Uri content. In the DataRequested event handler, set the Uri content using the SetUri method.

Now we are ready to share data, by clicking the share charm, when in the FullPageView.


 

Windows RT returned the mail app only. This is because among the applications currently on the system, Mail is the only app which supports Uri data. If we supported other types of data like Text, Bitmap, HtmlContent then there might be more applications in the list. In short it is a good practice to share as many types of data as possible. When Mail app is selected, the Mail app will be activated. The logic for using the data that we have shared is implemented within the target app, in our case the mail app.

In the next blog post I will discuss the logic for receiving the shared data.

Comments

  • Anonymous
    May 15, 2012
    Thanks for the info on the error, I got the exact same issue, and ended up with the same workaround.

  • Anonymous
    July 11, 2012
    I never encountered such an error, and I think I am lucky. This can be helpful for me if so. Thanks for thispost. I can't wait to get Windows 8. Felina Kindly visit my website, please <a href=www.aspirateurautomatique.org title="Aspirateur robot">click here</a>.

  • Anonymous
    July 18, 2012
    I didn't try it myself yet, but will this API also be available somehow to regular desktop applications?

  • Anonymous
    July 26, 2012
    Really nice article...

  • Anonymous
    November 08, 2012
    Hie.... thanks for the post  yup i'm getting this but for my app it is giving URL also i need to do is jst an image and the description so will u help me that share charm for send an image only.. waiting for ur reply....