Share Contract
DataTransferManager.ShowShareUI();
Implementing Share Source
protected override void OnNavigatedTo(NavigationEventArgs e) { // ... DataTransferManager.GetForCurrentView().DataRequested += OnShareDataRequested; } protected override void OnNavigatedFrom(NavigationEventArgs e) { // ... // Always remove your event handlers!!! DataTransferManager.GetForCurrentView().DataRequested -= OnShareDataRequested; } private void AppBarButton_Click(object sender, RoutedEventArgse ) { DataTRansferManager.ShowShareUI(; }
void OnShareDataRequested(DataTransferManager sender, DataRequestedEventArgs args) { var request = args.Request; request.Data.Properties.Title = "Share example"; // Mandatory!!! request.Data.Properties.Description = "This is a Demo Share example!"; // Not used 4 Mobile request.Data.SetText(TextToShare.Text.Trim(); }
Including Files in Data Package
async void OnShareDataRequested(DataTransferManager sender, DataRequestedEventArgs args) { var dp = args.Request.Data; // You need to use deferrals when there is an async operation in the request!!! var deferralo = args.Request.GetDeferral(); var photoFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/needle.jpg")); dp.Properties.Title = "Space Needle"; dp.Properties.Description = "Space Needle in Seattle, WA"; dp.SetStorageItems(new List<StorageFile> { photoFile }); dp.SetWebLink(new Uri("http://seattletimes.com/ABPub/2006/01/10/2002732410.jpg")); deferral.Complete(); }
Sample
<AppBarButton x:Name="ShareButton" Icon="ReShare" Label="Share" Click="ShareButton_Click"" />
private void ShareButton_Click(object sender, RoutedEventArgs e) { if(item.UserPhotos == null || item.UserPhotos.Count == 0) { var i = new MessageDialog("Select an Image").ShowAsync(); } else { // Start Shareing (does not show a UI) DataTransferManager.ShowShareUI(); } }
private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e) { // ... DataTransferManager.GetForCurrentView().DataRequested += OnShareDataRequested; } private void OnShareDataRequested(DataTransferManager sender, DataRequestedEventArgs args) { // ... }
OnShareDataRequested
Implementing Share Target
Register
// Manifest <Application ... > <Extensions> <uap:Extension Category="windows.shareTarget"> <uap:ShareTarget uap:Description="Share Target example"> <uap:SupportedFileTypes> <uap:FileType>.txt</uap:FileType> <uap:FileType>.jpg</uap:FileType> <uap:FileType>.png</uap:FileType> </uap:SupportedFileTypes> <uap:DataFormat>Text</uap:DataFormat> <uap:DataFormat>URI</uap:DataFormat> </uap:ShareTarget> </uap:Extension> </Extensions> </Application>
Formats
- Plain Text
- Formatted Text
- URI
- HTML
- Images
- Files
- Custom data formats
Share Target Activation
protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args) { Frame rootFrame = Windows.Current.Content as Frame; if(rootFrame == null) { rootFrame = new Frame(); Windows.Current.Content = rootFrame; } if(rootFrame.Content == null) rootFrame.Navigate(typeof(SharePage), args.ShareOperation); Windows.Current.Activate(); }
Extract Content to be Shared
private ShareOperation shareOperation; protected override async void OnNavigatedTo(NavigationEventArgs e) { shareOperation = e.Parameter as ShareOperation; TitleTextBlock.Text = shareOperation.Data.Properties.Title; DescriptionTextBlock.Text = shareOperation.Data.Properties.Description; SharedTextTextBlock.Text = await shareOperation.Data.GetTextAsync(); var photoFile = (StorageFile)(await shareOperation.Data.GetStorageItems Async())[0]; var imageSource = new Bitmap(); imageSource.SetSource(await photoFile.OpenReadAsync()); sharedPhotoImage.Source = imageSource; }
Exiting the Share Target
// Return User to source App. private void Button_CLick(object Sender, RoutedEventArgs e) { shareOperation.ReportCompleted(); }
Drag and Drop
Drag Source
<Grid AllowDrop="True" DragOver="Do_DragOver" Drop="Do_Drop" ...> ... </Grid>
<Border ...> <Image x:Name="DragImg" Source="Assets/DAELogo.png" CanDrag="True" DragStarting="DragImage_DragStarting" /> </Border>
Drag Target
<Button x:Name="copy" AllowDrop="True" DragOver="Copy_DragOver" DragLeave="Clear_DragLeave" Drop="Copy_Drop" Style="{StaticResource DropTargetButton}"> <Button.Content> <TextBlock Text=Drag here to copy" Style="{StaticResource DropTargetButtonText}"/> </Button.Content> <Button>
Sharing source fills DataPackage
private async void DragImage_DragStarting(UIElement Sender, DragStartingEvenetArgs args) { StorageFolder installFodler = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFolder subFolder = await installFolder.GetFolderAsync("Assets"); var file = await subFolder.GetFileAsync("DAELogo.png"); var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); args.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream)); var files = new List<StorageFile>(); files.Add(file); args.Data.SetStorageItems(files); args.Data.RequestedOperation = SetRequestedOperation(); }
Share Target unpacks DataPackage
private async void Copy_Drop(object Sender, DragEventArgs e) { TitleTextBlock.Text = e.DataView.Properties.Title; DescriptionTextBlock.Text = e.DataView.Properties.Description; SharedTextTextBlock.Text = await e.DataView.GetTextAsync(); var photoFile = (StorageFile)(await e.DataView.GetStorageItemsAsync())[0]; var imageSource = new BitmapImage(); imageSource.SetSource(await photoFile.OpenReadAsync()); sharedPhotoImage.Source = imageSource; }
Integrates with Built-In Apps Sharing
Built-In Sources
- Photos: Single Photo Viewer
- Web Pages: IE/Edge
- Music: XBox Music
- Video URI: XBox Music
- Documents: Office
- vCard file: Contacts
Built-In Targets
- Text/URI/Files: eMail
- Text/URI: Messaging
- URI/Files: Tap+Send
- Files: Bluetooth
- Text/URI/Pictures: OneNote