In the last post, we learnt “How to Save Contact in WP7 using the SaveContactTask?” Today in this small post we will learn how to retrieve saved contacts in WP7 using the Windows Phone 7 SDK APIs.

 

If you are developing any application for Windows Phone 7 and wants to provide user an option to get any contact details (e.g. Contact Name, complete Address of the Contact), AddressChooserTask class will allow you to get such information of the selected contact. Continue reading to get more details about this API.

 

Know About the API

AddressChooserTask is a sealed class present in the Microsoft.Phone.Tasks namespace. It derived from ChooserBase of type AddressResult. The class consists of only one method named Show() which will launch the Contact Chooser screen where user will be able to select the desired contact.

 

Here is the meta data of the AddressChooserTask:

 
namespace Microsoft.Phone.Tasks
{
    public sealed class AddressChooserTask : ChooserBase<AddressResult>
    {
        public override void Show();
    }
}

 

AddressChooserTask has a Completed event, where it will get the AddressResult as selected item. AddressResult class is also a sealed class present in the Microsoft.Phone.Tasks namespace and exposes two string properties named DisplayName and Address. DisplayName returns the name of the contact and the Address returns the complete address of the selected contact item.

 

Here is the meta data of the AddressResult class:

 
namespace Microsoft.Phone.Tasks
{
    public sealed class AddressResult : TaskEventArgs
    {
        public AddressResult();
        public AddressResult(TaskResult taskResult);
 
        public string Address { get; internal set; }
        public string DisplayName { get; internal set; }
    }
}

 

 

Implementation Steps

It is very easier to implement the address chooser task. Just create the instance of the class and register the Completed event. Finally call the Show() method which will display the specific UI in the screen.

 
// Create the instance of the AddressChooserTask
var addressChooserTask = new AddressChooserTask();
 
// Register for the Completed event
addressChooserTask.Completed += AddressChooserTask_Completed;
 
// Call the Show method to open the Contact List Screen
addressChooserTask.Show();

 

In the Completed event implementation, you can check the status of the task. If user chose a contact from the contact list, the TaskResult will come as "OK" and if the user cancels the selection process or interrupts the task, it will return as "Cancel".

 

Here is the simplest way of implementation of the AddressChooserTask_Completed event:

 
void AddressChooserTask_Completed(object sender, AddressResult e)
{
    switch (e.TaskResult)
    {
        case TaskResult.OK:
            // Fetch the selected Contact
            MessageBox.Show(e.DisplayName + "\n\n" + e.Address);
            break;
 
        case TaskResult.Cancel:
            // Contact Selection Operation Cancelled
            MessageBox.Show("Address Chooser Task interrupted by the user");
            break;
 
        default:
            break;
    }
}

 

Here is the screenshot of what you will see:

 

Image 1: How to Retrieve Contact Information in WP7 using the AddressChooserTask?   Image 2: How to Retrieve Contact Information in WP7 using the AddressChooserTask?   Image 3: How to Retrieve Contact Information in WP7 using the AddressChooserTask?

 

The first screen shows the Contact Chooser screen. When you select a contact from the list, you will get the Contact name and address as implemented above. In case you cancel and come back to the original page, it will come to the cancel case and show a message as implemented.

 

Hope this post was helpful to you. Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields.

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.