- Preparing Your Machine
- Creating a New Project
- Designing with Blend
- Adding Code
- Working with the Phone
- Where Are We?
Working with the Phone
This first application is a program that can be pretty self-sufficient, but not all applications are like that. Most applications will want to interact with the phone’s operating system to work with other parts of the phone. From within your application, you might want to make a phone call, interact with the user’s contacts, take pictures, and so on. The Windows Phone SDK calls these types of interactions tasks. Tasks let you leave an application (and optionally return) to perform a number of phone-specific tasks. Here is a list of some of the most common tasks:
- CameraCaptureTask
- EmailAddressChooserTask
- EmailComposeTask
- PhoneCallTask
- SearchTask
- WebBrowserTask
These tasks allow you to launch a task for the user to perform. In some of these tasks (for example, CameraCaptureTask, EmailAddressChooserTask), after the task is complete the user expects to return to your application; while in others (for example, SearchTask), the user might be navigating to a new activity (and might come back via the Back key, but might not).
Let’s start with a simple task, the SearchTask. Add a using statement to the top of the code file for Microsoft.Phone.Tasks to ensure that the SearchTask class is available to our code file. Next, create an event handler for the Tap event on theEllipse. Then, inside the handler for the Tap event, you can create an instance of the SearchTask, set the search criteria, and call Show to launch the task:
... using Microsoft.Phone.Tasks; ... public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { ... theEllipse.Tap += theEllipse_Tap; } void theEllipse_Tap(object sender, System.Windows.Input.GestureEventArgs e) { SearchTask task = new SearchTask(); task.SearchQuery = "Windows Phone"; task.Show(); } ... }
If you run your application, you’ll see that when you tap on the theEllipse element it will launch the phone’s Search function using the search query you supplied (as shown in Figure 2.33). The results you retrieve for the search query can vary because it is using the live version of Bing for search.

FIGURE 2.33. The SearchTask in action
Although this sort of simple task is useful, the more interesting story is being able to call tasks that return to your application. For example, let’s pick an email address from the phone and show it in our application. The big challenge here is that when we launch our application, we might get tombstoned (or deactivated). Remember that, on the phone, only one application can be running at a time. To have our task wired up when our application is activated (remember, it can be deactivated or even unloaded if necessary), we have to have our task at the page or application level and wired up during construction. So, in our page, we create a class-level field and wire up the Completed event at the end of the constructor for it, like so:
public partial class MainPage : PhoneApplicationPage { EmailAddressChooserTask emailChooser = new EmailAddressChooserTask(); // Constructor public MainPage() { ... emailChooser.Completed += emailChooser_Completed; } ... }
In the event handler, we can simply show the email chosen using the MessageBox API:
... void emailChooser_Completed(object sender, EmailResult e) { MessageBox.Show(e.Email); } ...
Now we need to call the task. To do this, let’s hijack the event that gets called when the theEllipse element is tapped. Just comment out the old SearchTask code and add a call to the emailChooser’s Show method, like so:
... void theEllipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { //SearchTask task = new SearchTask(); //task.SearchQuery = "Windows Phone"; //task.Show(); // Get an e-mail from the user's Contacts emailChooser.Show(); } ...
After you run the application, a list of contacts will be displayed and you will be able to pick a contact (and an address, if there is more than one), as shown in Figure 2.34. The emulator comes prepopulated with several fake contacts to test with.

FIGURE 2.34. Choosing a contact to retrieve an email address via the EmailAddressChooserTask
After the user selects the contact, the phone returns to your application. You will be returned to your application (and debugging will continue). The event handler should be called when it is returned to the application, as shown in Figure 2.35.

FIGURE 2.35. Showing the selected email in a MessageBox