- 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 may want to make a phone call, interact with the user's contacts, take pictures, and so on. The Windows Phone SDK 7.1 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 (e.g., CameraCaptureTask, EmailAddressChooserTask), once the task is complete the user expects to return to your application; while in others (e.g., SearchTask) the user may be navigating to a new activity (and may come back via the Back key, but may 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 make sure the SearchTask class is available to our code file. Next, create an event handler for the MouseLeftButtonUp event on theEllipse. Then, inside the handler for the MouseLeftButtonUp 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.MouseLeftButtonUp += new MouseButtonEventHandler(theEllipse_MouseLeftButtonUp); } void theEllipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs 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.34). The results you retrieve for the search query may vary as it is using the live version of Bing for search.
Figure 2.34 The SearchTask in action
While 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 may get tombstoned (or deactivated). Remember that, on the phone, only one application can be running at a time. In order 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, 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 += new EventHandler<EmailResult>(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 highjack 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(); } ...
Once you run the application, a list of contacts will be shown and you will be able to pick a contact (and an address, if there is more than one), as shown in Figure 2.35. The emulator comes prepopulated with several fake contacts to test with.
Figure 2.35 Choosing a contact to retrieve an email address via the EmailAddressChooserTask
Once the user picks 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.36.
Figure 2.36 Showing the selected email in a MessageBox