Working with Windows Phone User Interfaces, Part 2
In Part 1 of this article you learned how to work with orientation changes on the Windows Phone and how to create horizontally scrolling pages using Panorama and Pivot pages. In Part 2 you’ll see how to interact with some of the built-in applications on the phone through the use of the Launcher and Chooser applications. In addition, you will learn to create a common task bar for your Windows Phone using the Application Bar. This article assumes that you have Visual Studio 2010 and the Windows Phone tools installed along with it. You must download and install the Windows Phone tools separately to use them with Visual Studio 2010. You may also download the free Visual Studio 2010 Express for Windows Phone developer environment. Launchers The Windows Phone does not support multi-tasking (yet); however, you can interact with many of the built-in applications on the phone from within your application. You can interact with the built-in applications on the phone in two ways: passing in data, or get back data. A Launcher type of application lets you pass data from your application into the built-in phone application. In a Chooser type of application you will use a built-in phone application to select some data that is stored on your phone and return that data to your application. | " | The Windows Phone does not support multi-tasking (yet); however, you can interact with many of the built-in applications on the phone from within your application.
| " |
Using a Launcher When you use the Launcher API on the Windows Phone, your application calls one of the built-in applications. The user will complete a task, and then control will return to your application. No data will be returned from a Launcher application; however, your application may pass in some data to the Launcher. As an example of using a Launcher, you might ask a user to input a phone number, and then you pass that phone number to the SavePhoneNumberTask as shown in Figure 1. If the user saves that phone number, a return result of OK will be returned, but no other data.  Figure 1: Enter a Phone Number and then call the SavePhoneNumberTask Launcher.One important thing to keep in mind when using either the Launcher or Chooser API is that calling these applications could cause your application to be “tombstoned.” It will be your responsibility to restore any data that you want after your application is reactivated from the tombstoned state. | " | One important thing to keep in mind when using either the Launcher or Chooser APIs is that calling these applications could cause your application to be “tombstoned.”
| " |
Built-In Launchers Windows Phone contains several built-in applications that your C# or Visual Basic code can launch by instantiating an instance of the appropriate class. You can find all of the Launcher classes in the Microsoft.Phone.Tasks namespace. The list below contains the Launcher classes you can use on the Windows Phone. - EmailComposeTask
- MarketplaceDetailTask
- MarketplaceHubTask
- MarketplaceReviewTask
- MarketplaceSearchTask
- MediaPlayerLauncher
- PhoneCallTask
- SearchTask
- SmsComposeTask
- WebBrowserTask
To use any of the Launcher task classes, you first need to add a using statement at the top of your Page class. using Microsoft.Phone.Tasks;
Next, you create a field in your Page class with a variable that will point to the specific Launcher you wish to use. SavePhoneNumberTask _Task;
Now create an instance of the class in the constructor for your page and hook up the Completed event. public LauncherPage() { InitializeComponent(); _Task = new SavePhoneNumberTask(); _Task.Completed += new EventHandler<TaskEventArgs>(_Task_Completed); }
You must create the instance of the task and hook up the Completed event handler in the constructor because your application will be tombstoned once it calls the Launcher application. Upon reactivating your application, the constructor will be called again and the completed event needs to be hooked back up so the Launcher can call the event. In Figure 1 the user is asked to put a phone number into a text box named “txtPhone” on this page. When you click on the Save Phone Number button the Click event will fire. At this point you take the value from the txtPhone.Text property and set the PhoneNumber property on the SavePhoneNumberTask object. Now you call the Show() method of the task object and the Launcher application will run and receive the phone number from your application. Once the new application is running, your application will be tombstoned. private void Button_Click(object sender, RoutedEventArgs e) { // Pass phone number into contacts _Task.PhoneNumber = txtPhone.Text; // Display contact info app // This WILL tombstone your app _Task.Show(); }
In the Completed event you will want to check the e.TaskResult to see if the user accomplished the task. For example, when saving a phone number, if they do not actually click the save button after adding the new phone number/contact, then the result will not come back as OK. Each Launcher may report different results depending on what each one is supposed to do. In the Completed event procedure below you will simply display a message box telling the user that the phone number was saved. void _Task_Completed(object sender, TaskEventArgs e) { if (e.TaskResult == TaskResult.OK) MessageBox.Show("Phone Number Saved"); }
|