- Sourcing Content for Sharing
- Receiving Content as a Share Target
Receiving Content as a Share Target
The flipside to sharing is receiving content. The sample application referred to earlier in this chapter provides several examples of consuming shared content. To add support as a Share Target, simply right-click the project in the Solution Explorer and choose Add[ra]New Item and then select the Share Target Contract option. An example of this is shown in Figure 3.
Figure 3 Adding the Share Target contract to your application
The template will perform several tasks. It will add a page that acts as the target for the share. You use this page to prompt the user for any necessary information needed to complete the sharing operation. For example, if you have the Puzzle Touch application installed (you can obtain this free application from the Windows Store) and you share an image, you will be prompted to pick the size and difficulty of the puzzle. This is shown in Figure 4.
Figure 4 Share prompt for Puzzle Touch
The template will also update the manifest for your application. It adds a Share Target declaration and by default will support the text and uri data formats. It will display the title and description, prompt the user for a comment, and provide a button to complete the Share operation. You can see the result of this in Figure 5.
Figure 5 The Share Target declarations
The BlogPostTarget application for Chapter 8 demonstrates how to handle the receipt of custom data. When the Share operation begins, the application is activated from the Share Target contract. The code to handle this is automatically added to the App class by the template:
protected override void OnShareTargetActivated( ShareTargetActivatedEventArgs args) { var shareTargetPage = new ShareTargetPage(); shareTargetPage.Activate(args); }
The Activate method on the ShareTargetPage control loads information from the data package. Listing 2 shows the default code to obtain various properties and set them for data-binding.
Listing 2: Boilerplate Code to Load the Contents of a Share Operation
_shareOperation = args.ShareOperation; DataPackagePropertySetView shareProperties = _shareOperation.Data.Properties; var thumbnailImage = new BitmapImage(); DefaultViewModel["Title"] = shareProperties.Title; DefaultViewModel["Description"] = shareProperties.Description; DefaultViewModel["Image"] = thumbnailImage; DefaultViewModel["Sharing"] = false; DefaultViewModel["ShowImage"] = false; DefaultViewModel["Comment"] = String.Empty; DefaultViewModel["SupportsComment"] = true; Window.Current.Content = this; Window.Current.Activate(); if (shareProperties.Thumbnail != null) { var stream = await shareProperties.Thumbnail.OpenReadAsync(); thumbnailImage.SetSource(stream); DefaultViewModel["ShowImage"] = true; }
To receive the custom data format that is shared by the Wintellog3 application, the code in BlogPostTarget checks to see if the data format exists in the package, using the same schema identifier that was used to save the data:
const string BLOG_POST = "http://schema.org/BlogPosting"; if (!_shareOperation.Data.Contains(BLOG_POST)) return;
If the format exists, the data is extracted and checked to ensure it is not empty:
var data = await _shareOperation.Data.GetDataAsync(BLOG_POST); if (data == null) return;
Finally, the data is deserialized to the original format and saved. The same tool that was used to serialize the data (Json.NET) is used to perform the reverse operation. The example uses an anonymous type, so a template is passed to Json.NET for it to use. The template mirrors the data that is saved in the Wintellog3 application. Listing 3 shows the steps involved.
Listing 3: Loading Custom Data from the Share Operation
DefaultViewModel["ShowBlog"] = true; DefaultViewModel["BlogPost"] = Newtonsoft.Json.JsonConvert .DeserializeAnonymousType((string) data, new { type = "http://shema.org/BlogPosting", properties = new { description = string.Empty, image = new Uri("http://schema.org/"), name = string.Empty, url = new Uri("http://schema.org/"), audience = "Windows 8 Developers", datePublished = DateTime.Now, headline = string.Empty, articleBody = string.Empty } });
When the custom blog post information has been extracted, data-binding is used to display it to the end user. Listing 4 shows the XAML used to provide a clickable title (tapping the title will open the blog post in the default browser, typically Internet Explorer) and a scrollable section with the article content.
Listing 4: XAML to Show the Blog Post
<StackPanel Visibility="{Binding ShowBlog, ➥Converter={StaticResource BooleanToVisibilityConverter}}" Orientation="Vertical"> <HyperlinkButton NavigateUri="{Binding BlogPost.properties.url}" Content="{Binding BlogPost.properties.headline}"/> <ScrollViewer Height="200"> <RichTextBlock Margin="20"> <Paragraph> <Run FontWeight="SemiLight" FontSize="13" Text="{Binding ➥BlogPost.properties.articleBody}"/> </Paragraph> </RichTextBlock> </ScrollViewer> </StackPanel>
You can see the result of a Share operation in Figure 6. Notice the title and description are displayed, followed by the title as a hyperlink and the content of the article. The application will successfully display this content from any other application that uses the same schema.
Figure 6 Sharing a blog post
The Share contract is a powerful feature that allows sharing of content between applications. Your application can expose information without knowing what other applications are installed. This enables you to support advanced scenarios such as posting content to Twitter or Facebook without having to implement the code yourself. When the user installs an application that supports social media as a Share Target, your application is only a few taps away from posting the content online.