The email address chooser task launches the Windows Phone Contacts application and allows the user to select a contact. When the user completes the task, the Completed
event is raised, and the event handler receives an EmailResult
object that exposes a string containing the selected contact’s email address.
Note
To query the user’s contact list, without requiring the user to select a contact, use the Contacts
class, which is described later in this chapter.
The EmailAddressChooserTask
should be defined as a field in your class, like so:
readonly EmailAddressChooserTask emailAddressChooserTask
= new EmailAddressChooserTask();
Subscribe to the EmailAddressChooserTask.Completed
event within your class constructor, as shown:
emailAddressChooserTask.Completed
+= new EventHandler<EmailResult>(HandleEmailAddressChooserTaskCompleted);
Use the Show
method of the EmailAddressChooserTask
to launch the built-in Contacts app. When the task completes, the handler receives an EmailResult
object, as shown:
void HandleEmailAddressChooserTaskCompleted(object sender, EmailResult e)
{
if (e.TaskResult == TaskResult.OK)
{
ToAddress = e.Email;
}
else if (e.Error != null)
{
Message = "Unable to choose email address. " + e.ToString();
}
else if (e.TaskResult == TaskResult.Cancel)
{
Message = "Cancelled";
}
}
Example code for the EmailComposeTask
can be found in the EmailViewModel
in the downloadable sample code. The EmailView
allows the user to select the recipient and to launch a new EmailComposeTask
via a button (see Figure 14.5).
The viewmodel contains a public property of type ICommand
called ChooseRecipientCommand
. The view’s Choose Email button is bound to the command.
When the ChooseRecipientCommand
is executed, the Show
method of the emailAddressChooserTask
is called. This behavior is defined in the viewmodel constructor, as shown:
public EmailViewModel() : base("Email")
{
emailAddressChooserTask.Completed
+= HandleEmailAddressChooserTaskCompleted;
chooseRecipientCommand = new DelegateCommand(
obj => emailAddressChooserTask.Show());
...
}
When the EmailAddressChooserTask.Show
method is called, the built-in Contacts application is launched (see Figure 14.6).
The result returned by the Contacts app is used by an EmailComposeTask
, which is presented in the next section.
3.149.29.112