Monthly Archives: May 2016

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;                                  

            return true;

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

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.

Roaming and Local Settings in UWP

In the new Universal Windows Platform, you can store settings data in many ways. These days, there are multiple cloud options that you can communicate with; alternatively, you can store the settings in the user’s local profile. You can do this locally (that is, the data is stored on the current device against the current user only), or remotely (which means that the data will automatically be shared across all devices where your app is installed for that user). Both of these are stored as KeyValue Pairs.

Local Settings

Local settings are small, local variables. These are accessible even offline. The following code will only work on VS2015 because of the “?.” syntax; however, a null coalesce “??” will work just as well in older versions of VS.

string mySetting = Windows.Storage.ApplicationData.Current.LocalSettings.Values["MySetting"]?.ToString();

To set the setting, the syntax is basically the same:

Windows.Storage.ApplicationData.Current.LocalSettings.Values["MySetting"] = "1";

There is, as far as I’m aware, no limit to the amount of data that can be stored in these settings.

Roaming Settings

Roaming settings are a small, out of the box, solution for transferring settings across Windows devices. They aren’t new, and they are very easy to use; the following will return “MySetting” or null (VS2015+ only):

string mySetting = Windows.Storage.ApplicationData.Current.RoamingSettings.Values["MySetting"]?.ToString();

To set the setting, the syntax is basically the same:

Windows.Storage.ApplicationData.Current.RoamingSettings.Values["MySetting"] = "1";

The amount of data that can be stored here is apparently, around 100K. You can get the exact figure by interrogating the RoamingStorageQuota property:

Int storage = Windows.Storage.ApplicationData.Current.RoamingStorageQuota

Take this limit seriously; this article implies that, should you exceed it, all roaming will be stopped.

It’s also worth bearing in mind that you need to be online to access roaming storage.