OpenLightGroup Blog

rss

Blogs from OpenLightGroup.net


WinRT Sharing Made Easy - SoC style

I put together a little base class to make it an easy and repeatable process to add sharing to any WinRT application. The ability to share content using the share charm is not that difficult to begin with and full details can be found here: Sharing Content. However, I found it helpful to wrap this functionality up into separate classes that are only concerned with how to format the data to be shared. This could be especially helpful in large applications or applications that need to swap that sharing strategies due to application logic, or simply to save you a few lines of code each page and reuse code across pages.

Whatever your reason for interest, lets look at the nuts and bolts.

Here is the base class that I put together:

public abstract class BaseSharingManager : IDisposable
{
 
    protected DataTransferManager dataTransferManager;
    protected Func getItem;
 
    public void Subscribe(Func getItem)
    {
        this.getItem = getItem;
 
        this.dataTransferManager = DataTransferManager.GetForCurrentView();
 
        this.dataTransferManager.DataRequested += OnDataRequested;
    }
 
    public void Dispose()
    {
        if (dataTransferManager != null)
            this.dataTransferManager.DataRequested -= OnDataRequested;
    }
 
    protected abstract void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args);
 
}

Now you can create a child class that is responsible for share a particular type of data from your app. Here is an example of a class responsible for sharing a new item:

public class NewsSharingManager : BaseSharingManager
{
    protected override void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
    {
        var item = base.getItem();
 
        args.Request.Data.Properties.Title = "Z.A.S.T. News Item";
 
        args.Request.Data.Properties.Description = "Warn the world of the zombie apocalypse!";
 
        if (item != null)
        {
 
            if (!String.IsNullOrEmpty(item.Url))
                args.Request.Data.SetUri(new Uri(item.Url, UriKind.RelativeOrAbsolute));
 
            args.Request.Data.SetText(item.Title + "\r\n" + item.Description);
 
        }
    }
}

This now allows you to minimize the sharing code in the code behind pages of your app and to keep all the logic for data sharing in individual classes.

Here is how you use the manager in a WinRT page code behind:

private Sharing.NewsSharingManager shareManager = new Sharing.NewsSharingManager();
 
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    
    this.shareManager.Subscribe(() => this.itemListView.SelectedItem as Model.INewsItem);
}
 
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);
 
    this.shareManager.Dispose();
}

Do not forget to dispose of the share manager when you are navigating away from the page. If you do an exception will be thrown because an event handler is already registered with the DataTransferManager for the page.

Happy coding!





Comments are closed.