Written by Altınok Darıcı on 26 - Nov - 2014

Windows Phone 8.1 Share Contract - Share Source

Below code block is same for all type of sharing

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    DataTransferManager.GetForCurrentView().DataRequested -= OnShareDataRequested;
    base.OnNavigatedFrom(e);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    DataTransferManager.GetForCurrentView().DataRequested += OnShareDataRequested;
    base.OnNavigatedTo(e);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
    DataTransferManager.ShowShareUI();
}

As you see in above block we handle DataRequested event in current view using DataTransferManager class. There are several different implementation of OnShareDataRequested method, because of sharing type.

Sharing Text

private void OnShareDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    args.Request.Data.Properties.Title = "sample title";
    args.Request.Data.Properties.Description = "sample description";
    args.Request.Data.SetText("share me");
}

 The title field is mandorty.

Share Web Link with Files

private async void OnShareDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    List<StorageFile> files = new List<StorageFile>();
    var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Images/sample1.jpg"));
    files.Add(file);
    args.Request.Data.Properties.Title = "title";
    args.Request.Data.Properties.Description = "description";
    args.Request.Data.SetStorageItems(files);
    args.Request.Data.SetWebLink(new Uri("http://www.4sln.com/"));
    args.Request.GetDeferral().Complete();
}

Above block share file with web link.

There are different variation of OnShareDataRequested event with respect to data package data formats.

Method Comments
SetApplicationLink(Uri value) A Uri back to the source application (a Uri association)
SetBitmap(RandomAccessStreamReference value) A bitmap image
SetData(string formatId, object value) Used in a delayed rendering callback method to supply the data
SetDataProvider(string formatId, DataProviderHandler delayRenderer) Declares a callback method for delayed rendering of data items, if acquisition of data for sharing is time-consuming
SetHtmlFormat(string value) HTML content
SetRtf(string value) RTF formatted text
SetStorageItems(IEnumerable<IStorageItem> value, bool readOnly) One or more files and/or folders
SetText(string value) Simple text
SetWebLink(Uri value) Link to a resource on the network

 

References : http://msdn.microsoft.com/library/windows/apps/br205873.aspx

comments powered by Disqus