How to implement the "Share" button in a UWP project?
1 answer
Smoke Add Sharing to your UWP App article. At a minimum:
Add button:
<Page.TopAppBar> <CommandBar> <CommandBar.PrimaryCommands> <AppBarButton Label="Share" Click="AppBarButton_Click"> <AppBarButton.Icon> <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="& #xE72D;"/> </AppBarButton.Icon> </AppBarButton> </CommandBar.PrimaryCommands> </CommandBar> </Page.TopAppBar>To add the
ShowShareUIcall to the click handler:private void AppBarButton_Click(object sender, RoutedEventArgs e) { Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI(); }Add processing of request for shared data:
Windows.ApplicationModel.DataTransfer.DataTransferManager.GetForCurrentView() .DataRequested += MainPage_DataRequested; void MainPage_DataRequested(Windows.ApplicationModel.DataTransfer.DataTransferManager sender, Windows.ApplicationModel.DataTransfer.DataRequestedEventArgs args) { if(!string.IsNullOrEmpty(ContentText.Text)) { args.Request.Data.SetText(ContentText.Text); args.Request.Data.Properties.Title = Windows.ApplicationModel.Package.Current.DisplayName; } else { args.Request.FailWithDisplayText("Nothing to share"); } }
- well, cool feature: without a subscription to an event, calling the ShowShareUI method will not do anything. - Andrii Krupka
|