Home > C#, MVVM, WinRT > Adding Share support to ViewModel with MVVM Light in WinRT

Adding Share support to ViewModel with MVVM Light in WinRT

22/04/2013

Starting from version 1.5.0Caliburn.Micro adds support for sharing via ViewModel in a Windows Store app. But it is simple to implement the same feature using MVVM Light Toolkit.

First of all, we need an interface that must be implemented by ViewModels that want to act as sharing source:

public interface ISupportSharing
{
    void OnShareRequested(DataRequest dataRequest);
}

It defines only a method, OnShareRequested, that will be invoked when the user opens the Share charm. This task is accomplished by the following service class:

public class SharingService
{
    private readonly DataTransferManager transferManager;

    public SharingService()
    {
        transferManager = DataTransferManager.GetForCurrentView();
        transferManager.DataRequested += OnDataRequested;
    }

    private void OnDataRequested(DataTransferManager sender, 
        DataRequestedEventArgs args)
    {
        var view = this.GetCurrentView();
        if (view == null)
            return;

        var supportSharing = view.DataContext as ISupportSharing;
        if (supportSharing == null)
            return;

        supportSharing.OnShareRequested(args.Request);
    }

    private FrameworkElement GetCurrentView()
    {
        var frame = Window.Current.Content as Frame;
        if (frame != null)
            return frame.Content as FrameworkElement;

        return Window.Current.Content as FrameworkElement;
    }
}

When a share request starts, in the OnDataRequested event handler we retrieve the current view and check whether its DataContext (i.e., the ViewModel of the current View) implements the ISupportSharing interface. In this case, we invoke the OnShareRequested method on it, passing the DataRequest object.

Now we need to register this service when the app starts. So, define the following method in the ViewModelLocator class:

public static void RegisterSharingService()
{
    SimpleIoc.Default.Register<SharingService>(createInstanceImmediately: true);
}

And call it in the Application.OnWindowCreated method:

protected override void OnWindowCreated(WindowCreatedEventArgs args)
{
    ViewModelLocator.RegisterSharingService();
    base.OnWindowCreated(args);
}

Finally, we need to implement the ISupportSharing interface in all the ViewModels that want to share content. For example:

public class MainViewModel : ViewModelBase, ISupportSharing
{
    // ...

    public void OnShareRequested(DataRequest dataRequest)
    {
        dataRequest.Data.Properties.Title = "Share from ViewModel";
        dataRequest.Data.Properties.Description = "Using MVVM Light Toolkit";
        dataRequest.Data.SetUri(new Uri("https://marcominerva.wordpress.com"));
    }
}

If a ViewModel implements the ISupportSharing interface, but in a certain moment has nothing to share, we can simply return from the OnShareRequested method without setting any properties of the DataRequest object.

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