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.
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.