Starting from Windows 8, one big feature for Windows Store Apps is to share data using Charm Bar across Apps. Although the Charm Bar is removed in Windows 10, but the API for sharing is still in the fight.
For example, in my "Image Portray" App, I can share ink to OneNote or Outlook.
Today, I added same capability for my "Shanghai Metro" App. Here's how to do it.
First, to share data across Windows Store application, we need to use DataTransferManager
class. Create an instance of it in the page you want to add sharing.
DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
Because what we share is Images, so we will need a temporay file to save the Image data. The file model in WINRT API is StorageFile, create an object for that as well:
private StorageFile _tempExportFile;
Then, we need to register the DataRequested event handler on DataTransferManager. I would prefer do it in the page constructor:
dataTransferManager.DataRequested += DataTransferManager_DataRequested;
private async void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args) { try { DataPackage requestData = args.Request.Data; requestData.Properties.Title = "Your Title"; requestData.Properties.Description = "Your Description"; List<IStorageItem> imageItems = new List<IStorageItem> { _tempExportFile }; requestData.SetStorageItems(imageItems); RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(_tempExportFile); requestData.Properties.Thumbnail = imageStreamRef; requestData.SetBitmap(imageStreamRef); } catch (Exception ex) { await new MessageDialog(ex.Message, "ERROR").ShowAsync(); } }
In this code, requestData.Properties.Title is the title on the sharing UI, it will appear in the title bar on OneNote or in the subject text for Outlook.
requestData.Properties.Thumbnail is the thumbnail image, it requires type of RandomAccessStreamReference
The final step is to convert image into stream and give it to DataPackage: requestData.SetBitmap(imageStreamRef);
So, we need to copy our Image data and save it to a StorageFile in order to get the stream.
Just use my Edi.UWP.Helpers library, it is the most easy way to do!
1. Convert your Image file to WriteableBitmap
var rmbp = await Utils.LoadWriteableBitmap(PATH_TO_YOUR_IMAGE_FILE);
2. Create a temp file
StorageFile tempFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("ShanghaiMetro_Temp.png", CreationCollisionOption.ReplaceExisting);
3. Save the Image to the temp file
await rmbp.SaveStorageFile(tempFile);
How easy it is! You don't need to know any details, just use the API! I also open sourced the project here:
https://github.com/EdiWang/UWP-Helpers
Finally, by clicking the sharing button, pop up the share UI:
private async void BtnShare_OnClick(object sender, RoutedEventArgs e) { var rmbp = await Utils.LoadWriteableBitmap(CurrentStationMapUri.AbsolutePath.Substring(1, CurrentStationMapUri.AbsolutePath.Length - 1)); StorageFile tempFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("ShanghaiMetro_Temp.png", CreationCollisionOption.ReplaceExisting); await rmbp.SaveStorageFile(tempFile); _tempExportFile = tempFile; DataTransferManager.ShowShareUI(); }
Haseeb
i used this code but this does not show the all apps in my laptop they show just few apps please help me