You can tell which data formats your application can share. Just type in the name of the format, for example: “bitmap”;<\/li>\n<\/ul>\nHow to get the data ?<\/h3>\n
Once this done, it’s time to write some code. When an application is choose to initiate a sharing, its App.OnSharingTargetActivated method is called. You can then override it to create a custom control and activate it. Here is a sample example:
\n[csharp]protected override
\n void OnSharingTargetActivated(ShareTargetActivatedEventArgs args)
\n{
\n var shareTargetPage = new HelloWorld.SharingPage1();
\n shareTargetPage.Activate(args);
\n}
\n[\/csharp]<\/p>\n
Then in the custom control activation method, you can display it and process the data trough the ShareOperation property (returning a ShareOperation object):
\n[csharp]
\n public void Activate(ShareTargetActivatedEventArgs args)
\n {
\n if (args.Kind != ActivationKind.ShareTarget) return;
\n if (!ProcessArgs(args)) return;<\/p>\n
\/\/ Use the share button to commit the sharing request
\n ShareButton.Click += (backSender, backArgs) =>
\n {
\n \/\/ TODO: Perform application-specific work to accept the shared item
\n Window.Current.Close();
\n };<\/p>\n
Window.Current.Content = this;
\n Window.Current.Activate();
\n }
\n[\/csharp]<\/p>\n
The data processing is pretty easy too. There is a “Contains” method which can tell the developper if the shared object contains a specific type of data<\/strong>. You only have to provide a format string using the StandardDataFormats class.<\/p>\nThen you can retrieve the data using the exposed methods : GetBitmap, GetStorageItemsAsync, GetText, GetHtml, GetRtf or GetUri.<\/p>\n
Here is an exemple which display a provided bitmap in an image control of the sharing UI:
\n[csharp]private bool ProcessArgs(ShareTargetActivatedEventArgs args)
\n{
\n \/\/ does it contains an image ?
\n var data = args.ShareOperation.Data;
\n bool containsImage = false;
\n data.Contains(StandardDataFormats.Uri, out containsImage);
\n if (!containsImage) return false;<\/p>\n
\/\/Yes: display it !
\n var bitmapStream = data.GetBitmap();
\n BitmapImage bitmapImage = new BitmapImage();
\n bitmapImage.SetSource(bitmapStream);
\n image.Source = bitmapImage;
\n return true;
\n}[\/csharp]<\/p>\n
If you try this code, you’ll find out that it is not working because it keeps throwing an exception “Additional information: COM object that has been separated from its underlying RCW cannot be used.” \ud83d\ude41 Even the build sample does the same thing so I think this will be fixed soon… The quick work-around I found out is to use directly the args objects and do not create reference to any of it’s property…<\/p>\n
Freezing the UI is – really – not a good idea, especially in a Metro app so there is also async operation available to retrieve the data. This method is named GetDataAsync and here is an example of it’s use:
\n[csharp]GetDataOperation dataAsync = data.GetDataAsync(StandardDataFormats.Bitmap);[\/csharp]<\/p>\n
Reporting<\/h3>\n
The ShareOperation object exposes some methods which are here to reports the progress of the sharing to the user:<\/p>\n
\n- ReportCompleted: the sharing is finished succesfully !<\/li>\n
- ReportDataRetrieved: you have finished retrieving the data from the operation. This permits memory optimisation; OPTIONAL<\/li>\n
- ReportError(string value) : an error occured with a message for the user;<\/li>\n
- ReportStarted(): you started the sharing process; OPTIONAL<\/li>\n
- SetProgressText(string value): the sharing is in progress, providint a message for the user; OPTIONAL<\/li>\n<\/ul>\n
Quick links<\/h3>\n
One more thing (no, there is no iphone announcment :D) : quick links. They are a way to provide bookmarks of “sharing targeted people” currently named as “quick link”. Here is the description of this feature in the share sample of windows 8 :
\n“When reporting completed, you can optionally add a Quicklink to make it easier for the user to share to a specific person or group that they share with frequently. This saves them from having to select that person or group in your app every time they share to them.”<\/em><\/p>\nThis quicklink may be provided to the ReportCompleted. It is builded from an icon, a title, the supported files types (why ?), the supported formats (why ?) and an Id. <\/p>\n
Here is an example from the share sample:
\n[csharp]QuickLink quickLinkInfo = new QuickLink
\n{
\n Id = "ddd",
\n Title = "kikou",
\n SupportedFileTypes = { ".png", ".wma", ".jpg", ".bmp", ".wmv", ".txt",
\n ".mp3", ".mp4", ".gif", ".docx", ".pptx", ".xlsx", ".pdf" },
\n SupportedDataFormats = { "unicodetext", "uniformResourceLocatorW", "bitmap" }
\n};<\/p>\n
StorageFile iconFile = await Windows.ApplicationModel
\n .Package.Current.InstalledLocation
\n .CreateFileAsync("images\\\\user.png", CreationCollisionOption.OpenIfExists)
\n as StorageFile;
\nquickLinkInfo.Thumbnail = StreamReference.CreateFromFile(iconFile);
\n[\/csharp]<\/p>\n
Conclusion<\/h3>\n
This is a great feature which introduce some new way to improve the user experience. It is easy to implement but demands to be aware of some new concepts which have been seen here !<\/p>\n
If you want a complete demo you can download the windows 8 sample which is at this adress: http:\/\/code.msdn.microsoft.com\/windowsapps\/Sharing-Content-Target-App-e2689782<\/p>\n
Regards,<\/p>\n","protected":false},"excerpt":{"rendered":"
Metro applications introduce the use of contracts in your applications. You can think of them as “system interfaces” telling Windows: “my app is able to perform this operation“. One of them, and I think the…<\/p>\n","protected":false},"author":3,"featured_media":316,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[25,24],"_links":{"self":[{"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/posts\/289"}],"collection":[{"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/comments?post=289"}],"version-history":[{"count":33,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":554,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/posts\/289\/revisions\/554"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/media\/316"}],"wp:attachment":[{"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jonathanantoine.com\/wp-json\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}