Selecting a Contact’s Address Using the AddressChooserTask

,

The address chooser task is used to allow the user to provide your app with the street address of a contact. This task launches the built-in Contacts application so that the user can select a contact.


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.


If the user completes the task, an event is raised and the task’s Completed event handler receives an address in the result.

The sample for the AddressChooserTask consists of the AddressChooserTaskView page and AddressChooserTaskViewModel class.

The AddressChooserTask should be defined as a field in your class, like so:

readonly AddressChooserTask addressChooserTask = new AddressChooserTask();

Subscribe to the AddressChooserTask.Completed event within your class constructor, as shown:

addressChooserTask.Completed
                      += new EventHandler<AddressResult>(HandleCompleted);

Use the Show method of the AddressChooserTask to launch the built-in Contacts application (see Figure 14.24).

Image

FIGURE 14.24 Contacts application.

When the task completes, the handler receives an AddressResult object that contains the display name and address of the contact. See the following excerpt from the AddressChooserTaskViewModel class:

void HandleTaskCompleted(object sender, AddressResult e)
{
    if (e.Error != null)
    {
        MessageService.ShowError("Unable to retrieve address");
    }

    if (e.TaskResult != TaskResult.OK)
    {
        return;
    }

    DisplayName = e.DisplayName;
    Address = e.Address;
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.180.145