Home > C#, WinRT > A base class to simplify sharing task in Windows Store apps

A base class to simplify sharing task in Windows Store apps

12/03/2013

Everytime we want to share content from our Windows Store apps, we need to follow the same steps:

  1. obtain the DataTransferManager of the current window;
  2. register an event handler for the DataRequested event;
  3. provide the sharing content in that event handler.

Moreover, we must ensure that only a single event handler for DataRequest event is registered at any time, otherwise we’ll obtain an InvalidOperationException.

Applying this procedure leads to the writing of repetitive code. So, we can define a base class that takes care of all these aspects:

using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace App1.Common
{
    public abstract class ShareablePage : Page
    {
        private DataTransferManager dataTransferManager;
        private TypedEventHandler<DataTransferManager, DataRequestedEventArgs> 
            shareHandler;

        public ShareablePage()
        {
            dataTransferManager = DataTransferManager.GetForCurrentView();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            shareHandler = new TypedEventHandler<DataTransferManager, 
                DataRequestedEventArgs>
                ((s, args) =>
                {
                    Share(args.Request);
                });
            dataTransferManager.DataRequested += shareHandler;

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            dataTransferManager.DataRequested -= shareHandler;

            base.OnNavigatedFrom(e);
        }

        protected abstract void Share(DataRequest dataRequest);
    }
}

The ShareablePage class inherits from Page and overrides its OnNavigateTo and OnNavigatedFrom methods to provide base support for sharing:

  1. in the OnNavigatedTo method, we define the event handler for the DataRequested event;
  2. when the latter is raised, we invoke the Share method, that must provide the actual sharing content;
  3. in the OnNavigateFrom method, we remove the event handler, so that we are sure that the DataRequested event won’t be assigned more than one time.

Using this base class, all that we must do in our pages is to inherit from ShareablePage and override the Share method. Let’s begin modifying the XAML markup of the page we want to add sharing functionalities to. We must replace the Page tag with ShareablePage:

<common:ShareablePage
    ...
    xmlns:common="using:App1.Common"
    ...>

    ...
    
</common:ShareablePage>

Then, in the code-behind file, we must change the base class of the page and override the Share method:

public sealed partial class MainPage : ShareablePage
{
    // ...

    protected override void Share(DataRequest dataRequest)
    {
        var data = dataRequest.Data;
        data.Properties.Title = "Sharing title";
        data.Properties.Description = "Sharing description";

        data.SetText("This is the content I want to share.");
    }
}

If in your page you have already overridden the OnNavigateTo and OnNavigatedFrom methods, remember to call their base implementation, otherwise the code in SearchablePage class won’t be called.

That’s all. Now, everytime the user starts a sharing operation from our app, the Share method will be automatically invoked, without having to worry about handling the operation details.

Categories: C#, WinRT
%d bloggers like this: