Share Content in a UWP App

Sharing has changed slightly between Windows 8 and 10, but broadly speaking, the concept is the same. This article is pretty much where I worked most of this out from; however, there were some small pieces missing for my implementation:

Sharing Video Files

My target here was to share an avi file that I’d just created. My first step was to create a Share Helper (most of what is in here is described in the linked article):

    class ShareHelper
    {
        private IStorageItem _storageItem;

        internal bool Share(IStorageItem storageItem)
        {
            if (storageItem == null)
            {
                return false;
            }

            _storageItem = storageItem;

            DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
            dataTransferManager.DataRequested += DataTransferManager_DataRequested;                                  

            DataTransferManager.ShowShareUI();
            return true;
        }

        private void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            if (_storageItem == null) return;
                        
            DataRequest request = args.Request;                        
            
            List<IStorageItem> storage = new List<IStorageItem>()
            {
                _storageItem
            };
            request.Data.Properties.Title = "Share";
            request.Data.Properties.Description = "Share your movie!";
            request.Data.SetStorageItems(storage);
            
        }
    }

In Detail

The function SetStorageItems accepts an IEnumarable of IStorageItem. In this case I only have one thing to share, so I’ve just created an arbitrary list. The event handling seems a little overly complex for what it is.

Microsoft recommend that you don’t purposely call the ShowShareUI, but there are a number of situations – for example, the share icon in the camera app – that wouldn’t be intuitive any other way (I’m no UX expert, so I’d be happy to be corrected on this). However, the share UI still behaves as though you has swiped in from the right. To be honest, I kind of expected this Windows 8 chrome to be gone for Windows 10, some is clearly still alive and well.

Leave a Reply

Your email address will not be published. Required fields are marked *