Windows Phone 8.1 for Developers – Contracts

This blog post is part of a series about how Windows Phone 8.1 affects developers. The series is written in collaboration with Microsoft evangelist Peter Bryntesson, check out his blog here.


In this blog post I will do a quick overview of what is new with contracts in Windows Phone 8.1. This is the first impression of the functions so I do recommend you to take a look at MSDN if you want a deeper understanding of how it works.

File Open Picker contract

In Windows Phone 8.1 there has popped up a new functions named PickSingleFileAndContinue and PickMultipleFilesAndContinue. It is not async because the app will suspend if it needs to when calling these functions. This is because if the phone has low memory it will run out of it so the solution is to suspend your app when the picker is open and then resume it when it is done.

So how do one get hold of the selected file? You have to add code to the OnActivated function in App.xaml.cs. Here is an example of how to use PickSingleFileAndContinue:

private void Button_Click(object sender, RoutedEventArgs e)
    var openPicker = new FileOpenPicker
        ViewMode = PickerViewMode.Thumbnail,
        SuggestedStartLocation = PickerLocationId.PicturesLibrary
protected override void OnActivated(IActivatedEventArgs args)
    var root = Window.Current.Content as Frame;
    var mainPage = root.Content as MainPage;
    if (mainPage != null && args is FileOpenPickerContinuationEventArgs)
        mainPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs);
public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs fileOpenPickerContinuationEventArgs)
    if (fileOpenPickerContinuationEventArgs.Files != null)
        // Do something with selected file/s

File Save Picker contract

This works in very similar way as the open picker contract does.

Share contract

Windows Phone 8.0

The old way is pretty straight forward:

var shareLinkTask = new ShareLinkTask { Title = "my title", LinkUri = new Uri(""), Message = "my message"};

The share classes ShareLinkTask, ShareMediaTask or ShareStatusTask can be called from most places in your code and the show UI from the operation system will be shown.

Windows Phone 8.1


The new way of sharing is like the one in Windows store apps. Windows can ask the program if it has anything to share, and what that might be. For this you need to implement a function in your views which can share and set it in the DataTransferManager. A complete code might look like:

protected override void OnNavigatedTo(NavigationEventArgs e)
    // Register the current page as a share source.
    _dataTransferManager = DataTransferManager.GetForCurrentView();
    _dataTransferManager.DataRequested += OnDataRequested;

protected override void OnNavigatedFrom(NavigationEventArgs e)
    // Unregister the current page as a share source.
    _dataTransferManager.DataRequested -= OnDataRequested;

protected void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
    e.Request.Data.Properties.Title = "Some title";
    e.Request.Data.Properties.Description = "Some description"; // Optional
    e.Request.Data.SetUri(new Uri(""));

In the example we use the SetUri to share an Uri. There are many different shares which is used to share different things:

SetBitmap(RandomAccessStreamReference value)

SetApplicationLink(Uri value)

SetData(string formatId, object value)

SetDataProvider(string formatId, DataProviderHandler delayRenderer)

SetStorageItems(IEnumerable value)

SetStorageItems(IEnumerable value, bool readOnly)

SetText(string value)

SetUri(Uri value)

SetWebLink(Uri value)

SetHtmlFormat(string value)

SetRtf(string value)

If the OnDataRequested will take a little time you can use deferral to tell the operating system that your app has not stopped working. You can also call the share view from code to show it. Example if you have share in a menu and want to show it when user selects it. When you have to call this function from somewhere:


If you want to implement an app which will be a share target you need to specify what you want to receive in the appxmainifest. For example an app which want to receive pictures may look something like this:


When your app is selected to be a share target your app is started and you capture the shared object in OnShareTargetActivated which shall be set in App.xaml.cs. Example:

protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
    // Do something with args.ShareOperation.Data

Inside args.ShareOperation.Data there are many function to call to get the shared content. You already know what functions to use since this is stated in the appxmainfest. In the example above we would use args.ShareOperation.Data.GetStorageItemsAsync(). After the share is done call ReportCompleted to tell the system to return to the calling app.


Using file pickers is a bit harder than before but it is for a good cause that even low end devices will be able to work with Windows Phone 8.1.

Also share in Windows Phone 8.1 is a bit more tedious than before, but the up side is that it is more like Windows store apps and have all that benefits of share between apps and more. We can also share more thing than before.

This Post Has 18 Comments

  1. Christophe

    Hello, This code is good for a “Windows Phone 8.1” but it’s seems be different for a “Windows Phone 8.1 Silverlight”? have you a sample for this ?

    1. Robert Hedgate

      I guess the old way still works. Do as you did on Windows Phone 8.

        1. Robert Hedgate

          That is the downside of upgrading to Silverlight 8.1, not all API from WP 8.1 is available. I havn’t look into this very much so I can’t say for certain but if you havn’t found any answer on this at msdn it may not be possible.

  2. Anders

    I’ve noticed that ShareLinkTask doesn’t seem to work consistently on WP8.0 and WP8.1 with Facebook.

    On WP8.0 the title, description and link will appear on Facebook, but only the link will appear if used with 8.1.

    I’ve tried using the new method on 8.1, but setting text and then uri will only display the uri. The same behaviour can be seen by opening it in Message.

    How would you post a link to Facebook with the new system with the text and title as in 8.0?

  3. Satheesh

    how to pick media files like mp3 or mp4 using file picker???

    1. Robert Hedgate

      Change the start location to PickerLocationId.MusicLibrary or PickerLocationId.VideosLibrary and the filter to FileTypeFilter.Add(“.mp3”) or FileTypeFilter.Add(“.mp4”)

  4. Satheesh

    can you give the example code for it???

  5. WinitWindowsPhone8


    How to Show Whatsapp and Instagram app in share contract of Windows phone 8.1 Universal App?

    1. Robert Hedgate

      You can´t decide what apps show up in share. All apps which can receive the file type you are sharing will show up.

      I don´t know what file types WhatsApp or Instagram have registered as filetarget (if any).

      1. WinitWindowsPhone8


        I am sharing an image from my app using DataTransferManger. It shows apps like FB,Twitter,Ondrive but it not shown apps like Whatapp and Instagram.

        Is there any possibility to show them manually by writing any code using C#.

        Thanks in Advance.

        1. Robert Hedgate

          Not to my knowledge.

          You should contact WhatApp and Instagram and ask them to implement share target.

          1. WinitWindowsPhone8

            Thanks for your quick response.

  6. WinitWindowsPhone8


    I am sharing an image from my app using DataTransferManger. It shows apps like FB,Twitter,Ondrive but it not shown apps like Whatapp and Instagram.

    Is there any possibility to show them manually by writing any code using C#.

    Thanks in Advance.

  7. Andrea

    Hi, is there a method to share audio file in Windows phone 8.1?

  8. Peyman

    thanks it’s really help me. <3

Leave a Reply