The phone number chooser task is used to launch the built-in Contacts application and allows the user to select a contact. A phone number for the contact is then returned to your app.
Note
To query the user’s contact list without requiring the user to select a contact, use the Contacts
class, described later in this chapter.
The PhoneNumberChooserTask
should be defined as a field in your class, like so:
readonly PhoneNumberChooserTask phoneNumberChooserTask
= new PhoneNumberChooserTask();
Subscribe to the PhoneNumberChooserTask.Completed
event within your class constructor, as shown:
PhoneNumberChooserTask.Completed
+= new EventHandler<PhoneNumberResult>(
HandlePhoneNumberChooserTaskCompleted);
Use the Show
method of the PhoneNumberChooserTask
to launch the built-in Contacts app. When the task completes the handler receives a PhoneNumberResult
object, as shown:
void HandlePhoneNumberChooserTaskCompleted(object sender,
PhoneNumberResult e)
{
if (e.TaskResult == TaskResult.OK)
{
PhoneNumber = e.PhoneNumber;
}
else if (e.Error != null)
{
Message = "Unable to choose number. " + e.ToString();
}
else if (e.TaskResult == TaskResult.Cancel)
{
Message = "Cancelled";
}
}
Example code for the PhoneNumberChooserTask
can be found in the PhoneCallViewModel
in the downloadable sample code. The PhoneCallView
allows the user to populate the Phone Number TextBox
by using a button that launches the PhoneNumberChooserTask
(shown previously in Figure 14.14).
A button in the view is bound to a viewmodel ICommand
property named ChooseCommand
. The chooseCommand
field is instantiated in the PhoneCallViewModel
constructor. When executed, the command calls the Show
method of the PhoneNumberChooserTask
, as shown:
public PhoneCallViewModel() : base("Phone Call Tasks")
{
phoneNumberChooserTask.Completed
+= HandlePhoneNumberChooserTaskCompleted;
chooseCommand = new DelegateCommand( obj => phoneNumberChooserTask.Show());
...
}
When the Show
method is called, the Contacts application is launched (see Figure 14.16).
When the user selects a contact from the Contacts app, the sample app is activated, and the Completed
event handler is called, which updates the PhoneNumber
property in the viewmodel.
3.137.199.182