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.

Introduction

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:

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:

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

Share

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:

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

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:

Receive

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:

clip_image002

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:

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.

Summary

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. 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. I guess the old way still works. Do as you did on Windows Phone 8.

        1. 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. 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. how to pick media files like mp3 or mp4 using file picker???

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

  4. can you give the example code for it???

  5. Hi

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

    1. 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. Hi

        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. Not to my knowledge.

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

          1. Thanks for your quick response.

  6. Hi

    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. Hi, is there a method to share audio file in Windows phone 8.1?

  8. thanks it’s really help me. <3

Leave a Reply

Close Menu