Search code examples
windowsuwpwindows-phoneshare

How to add Share button to the CommandBar of a Windows 10 Mobile UWP App and sharing capabilities to it?


  1. I'm trying to add a button to the command bar of a phone app (8.1 and UWP) with a typical Windows Phone share icon. How do I add such an icon to it?

  2. I'd also like it to bring up the sharing window where the user can select the different ways of sharing something. It would share the link to my app in the store, like from the store app.


Solution

  • 1: There is no share charm in Windows 10. What you can do is that you do the icon yourself. This would make it:

    <AppBarButton Label="Share"  >
        <AppBarButton.Icon>
            <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE72D;"/>
        </AppBarButton.Icon>
    </AppBarButton>
    

    2:

    In UWP I would use Share contract. (Basically the DataTransferManager class…)

    Here is a good documentation for that: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/mt243293.aspx

    Basically:

    Hook up the event (e.g. in your page’s constructor..):

    DataTransferManager.GetForCurrentView().DataRequested += MainPage_DataRequested;
    

    Show the UWP share UI (e.g. on the button’s event handler..)

    DataTransferManager.ShowShareUI();
    

    Do the sharing:

    private void MainPage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
    {
           args.Request.Data.SetWebLink(URI); 
           args.Request.Data.Properties.Title = "My new title";
           args.Request.Data.Properties.Description = "description";
    }