How to implement the "Share" button in a UWP project?

1 answer 1

Smoke Add Sharing to your UWP App article. At a minimum:

  1. 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> 
  2. To add the ShowShareUI call to the click handler:

     private void AppBarButton_Click(object sender, RoutedEventArgs e) { Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI(); } 
  3. 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