Task System in Windows Phone 7

In this article I will be talking about Task System in Microsoft.Phone.Tasks namespace.

In Windows Phone 7, there are some mini built-in applications that can help you achieve some goals preventing & saving you for efforts writing extra codes.

These mini built-in applications are combined together as "Tasks". They can be called easily and you can return the data from these applications and import them into your applications. It doesn't matter whether you use XNA or Silverlight. You just need to add a reference to Microsoft.Phone and import Microsoft.Phone.Tasks into your namespace declaration.

These tasks include (which I will talk about in this article)
  • Camera Capture Task
  • Email Address Chooser Task
  • MarketPlace Search Task
  • MarketPlace Hub Task
  • MediaPlayer Launcher
  • Phone Call Task
  • Phone Number Chooser Task
  • Photo Chooser Task
  • Search Task
  • SMS Compose Task
  • WebBrowser Task
Note: I prefered using XNA in this article but that doesnt mean you can't use Silverlight. Its up to your choice.

Camera Capture Task:

This task initializes Camera Capture application. It takes your photos. But as long as we use emulator, we won't get a picture; only a black screen.

Lets write some code to see how it's done.

Declare a new variable:

CameraCaptureTask cct = new CameraCaptureTask();

Add these codes anywhere:

cct.Show();
cct.Completed += new EventHandler<PhotoResult>(cct_Completed);

Add this Eventhandler:

public void cct_Completed(object sender, PhotoResult e)
{
    Debug.WriteLine("File Name: " + e.OriginalFileName);
    Debug.WriteLine("Length: " + e.ChosenPhoto.Length + " bytes");
}

Lets run and see what happens:

1.gif
 
This is actually a Camera Capture Application. Ignore the blackness in the screen. It's black because I'm using Emulator.

When you click this symbol:

2.gif

It will instantly take the photo.

3.gif
 

You can see it on "Accept" and "Retake" buttons below. If you Retake it will show you the Camera Capture Task again. If you click Accept than our windows phone app will get focus. Think these Tasks as "Processes" in Windows Development. You can call and get data from these applications. Same in WP7 too.

4.gif
 

This symbol is for Zooming. You can Zoom in or Zoom out using these symbols.

Allright after clicking Accept we're getting some values from Debug.Writeline:

Length: 5845 bytes

That means:

Although there is no photo taken (in emulator) it somehow stored the size of the picture taken.

You can take a photo of anything then set the OriginalFileName to a variable in your application and use it how you want to.

Camera functionality adds interactivity to your applications/games.

Use it wisely

Email  Address  Chooser  Task:

This task enables you choose an email address from your contacts.

Lets see how its done.

Declare a new variable:

EmailAddressChooserTask ect = new EmailAddressChooserTask();

Add these codes anywhere:

ect.Completed += new EventHandler<EmailResult>(ect_Completed);
ect.Show();

Add this Eventhandler:

public void ect_Completed(object sender, EmailResult e)
{
  if (e.TaskResult == TaskResult.OK)
  {
      Debug.WriteLine(e.Email);
  }
  else
  {
  }
}

This application gets the selected contact's email address. Let's run the project and see the result.

5.gif
 
This is how Email Address Chooser looks like. There are a few contacts added before.

If we click one of the contacts (let's assume I have selected Julia Ilyina)

The Result is:


This is her email address. 

Thats it. Very simple.

MarketPlace Search Task:

MarketPlace Search Task helps you to search Music or Applications through WP7 Marketplace.

You just need to define the search terms and set Content Type to Music or Applications

First of all create an instance of MarketplaceSearchTask:

MarketplaceSearchTask mst = new MarketplaceSearchTask();

After that add this code:

mst.ContentType = MarketplaceContentType.Music;
mst.SearchTerms = "usain";
mst.Show();

This will search Music Albums about Usain Bolt.

Run the project and see what happens.

6.gif
 
If you came across this screen, delete a letter and rewrite it.

Sometimes we may have this kind of problem.

7.gif
 

Here are our search results. When you click one of them, you can see the albums details and price.

8.gif
 

By clicking the image with "Play", you will be listening to the 30 seconds of the song.

9.gif
 

MarketplaceSearch Task is great as you can see.

MarketPlace Hub Task:

Another MarketPlace Task is MarketPlaceHubTask.

This task shows you the Zune MarketPlace. It works like a Panorama Application (one of the Silverlight-WP7 Project Templates)

Let's write some code to make it work.

Declare this task instance first:

MarketplaceHubTask mht = new MarketplaceHubTask();

Then call it using:

mht.ContentType = MarketplaceContentType.Music;
mht.Show();

This will show you the Zune MarketPlace.

Lets run it and see how it looks like.

10.gif
 

As I said before this task looks like the Panorama Application in Silverlight-WP7 Applications.

1st page always shows us the Artist of the Week.

This week is for: Rihanna!

11.gif
 
Here are the other headlines from Zune Marketplace

12.gif
 
This page is for New Releases. By clicking them you can view their details.

Remember that you need to Sign in using your Live ID to view Details and listen to some music or buy the songs you like.

13.gif
 

Here are the Top Albums in ZUNE Marketplace.

14.gif
 

And you can of course filter the music genres.

A nice feature in my opinion.

This task allows you to use Zune Marketplace.

MediaPlayer Launcher:

Lets see some real action. Shall we?

MediaPlayer Launcher is one of the most nice tasks in Task System.

Let me remind you that you won't be able to see full videos using MediaPlayer Launcher because of the Emulator limitations. When you run it, after 1-2 second later it will go black. But you will be able to listen to the sound of the video files you are playing.

Let's see how it's done.

First declare this instance of MediaPlayerLauncher;

MediaPlayerLauncher mpl = new MediaPlayerLauncher();

Then use these codes to call it:

mpl.Controls = MediaPlaybackControls.All;
mpl.Location = MediaLocationType.Install;
mpl.Media = new Uri("http://www.samplewebsitehere.com/sample.wmv"); //This is a sample.Please change it as your needs.
mpl.Show();

You can set MediaPlayBackControls which are: All, FastForward, None, Pause, Rewind, Skip and Stop.

Location enumeration has 3 values: Data, Install and None.

Data plays the specified Media file from IsolatedStorage; Install plays the specified Media file from the Installation directory.

As we have used Uri you can also get the media file from the internet. No need to add media file to installation directory.

Phone  Call  Task:

This is just great. Think of it! You can call your friends while using the application or playing your game.

Implementation is so easy. As are all the tasks!

Declare an instance of PhoneCallTask:

PhoneCallTask pct = new PhoneCallTask();

And use these codes to call:

pct.DisplayName = "Ibrahim Ersoy";
pct.PhoneNumber = "05556549556"; //This is a fake number dont call :)
pct.Show();

Lets run the application:

15.gif

Here is what it looks like. It needs confirmation whether you wish to dial me or not.

If you click don't call, the task will end.

Choose call and lets explain the new window:

16.gif
 

Currently you're talking to me using Fake GSM Network. Nothing happens.

17.gif
 
This button shows you digits.
 
18.gif
 

19.gif

And this button adds extra options to this task:

20.gif

You can increase/decrease the volume, hold the caller or call another person.

Phone Number Chooser Task:

This task allows you to select a number from your contacts.

Let's see how to implement it.

First create a new instance of PhoneNumberChooserTask:

PhoneNumberChooserTask pnct = new PhoneNumberChooserTask();

Then add this code to call it:

pnct.Completed += new EventHandler<PhoneNumberResult>(pnct_Completed);
pnct.Show();

Add this EventHandler:

public void pnct_Completed(object sender, PhoneNumberResult e)
{
    Debug.WriteLine(e.PhoneNumber);
}

Let's run it.

21.gif
 

I have chosen Chris Sells and it returned this value: (206)555-0003.

It's the phone number of Chris Sells

Photo Chooser Task:

This task allows us to select a photo or take a photo from camera. Useful if you're looking for how to share your photos with your friends.

First lets create an instance of PhotoChooserTask:

PhotoChooserTask pct = new PhotoChooserTask();

Add this code to call it:

pct.Completed += new EventHandler<PhotoResult>(photochooser_Completed);
pct.Show();

And finally add that Eventhandler:

public void photochooser_Completed(object sender, PhotoResult e)
{
    Debug.WriteLine("Size: " + e.ChosenPhoto.Length + " bytes");
    Debug.WriteLine("File Name: " + e.OriginalFileName);
}

Let's run and see the results:

22.gif
 
As I explained above, you can take a photo or select the photos from your archive.

Let's select a photo:

23.gif
 

And see the result:

Size: 85524 bytes
File Name: \Applications\Data\314F9DB7-9953-48CE-847B-A4D5FE25B058\Data\PlatformData\PhotoChooser-168c35b3-a7e3-4355-9124-ca3d25b93481.jpg

Search Task:

This task allows you to search any term using Bing Search Engine in all over the internet, on your local machine or in news.

Let's see how its done.

First of all create a new instance of SearchTask:

SearchTask st = new SearchTask();

Then add these codes to call the task:

st.SearchQuery = "iersoy xna";
st.Show();

and see the results:

24.gif
 

When you first run SearchTask, it informs you about localized search process. Click Allow and Continue.

25.gif
 

Here are the results using Bing Search Engine on the Internet. There are other options which are local and news.

Since the WP7 Emulator can't get my local information(limitation), there won't be any search result.

Let's take a look at News:

26.gif
 

News gathers all the information from RSS Feeds of most-read and new articles according to our search criteria on all over the internet. When we write down "XNA" it will give us news feeds about XNA.

SMS  Compose  Task:

This task allows you to send SMS.

Here is how to implement it.

Create a new instance of SmsComposeTask:

SmsComposeTask smsct = new SmsComposeTask();

Then add this code to call it:

smsct.Show(); 

By adding SMS functionality, you're able to send text messages or text messages with pictures attached.

Lets see how it looks like:

27.gif
 

In  "To" section you can add your message receivers or by clicking "+" rounded button at the right of the text, you can select people from your contact list.

When you enter someone, the textbox works like an AutoCompleteBox, so let's try Chris Sells (which is in our emulator's Contacts):

28.gif
 
As it looks like.

As you already know that: write a message at "type a message" labelled textbox below the "To" section

29.gif
 
And then click on the "Attachment" image:
 
30.gif

31.gif
 
This new screen will allow us to attach a picture to sms. You can taske a new photo by clicking "camera" icon below or choose "7" labelled football playing kid (the photo which will be added in our sms)

Lets choose this kid and add it on our sms,

32.gif
 

By the way the kid isn't my son. Its just a sample text message for demonstration.

Now click on this image to send it:

33.gif
 

Because of we use a fake gsm network (using emulator), there will be problem telling us:

34.gif
 

Thats it! Now you know how to send sms using "SMS Compose Task".

If you like you can delete the conversation by clicking "..." labeled button below this message and click "delete conversation".

36.gif

Web Browser Task:

Our last task is Web Browser Task. I believe you don't need a fully detailed description of what this task does, but to mention the only thing this task does is to "Show a Webpage".

Let's implement this task.

Create a new instance of WebBrowserTask:

WebBrowserTask wbt = new WebBrowserTask();

And write down this code to call it:

wbt.URL = "http://www.csharpcorner.com";
wbt.Show();  

Let's see the result:

37.gif
 

Thats it!

Hope this article was useful for you. You can interact with the Task System within your applications as described in this article. I believe your applications will be much more dynamic by doing so.

I will be writing much more about Windows Phone 7 Development. So stay tuned.

Cheers!


Similar Articles