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 want 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 the 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 much 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.
var addressChooserTask = new AddressChooserTask();
addressChooserTask.Completed += AddressChooserTask_Completed;
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:
MessageBox.Show(e.DisplayName + "\n\n" + e.Address);
break;
case TaskResult.Cancel:
MessageBox.Show("Address Chooser Task interrupted by the user");
break;
default:
break;
}
}
Here is the screenshot of what you will see:
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 and tricks on various technology fields.
Reference: http://www.kunal-chowdhury.com
You may like to follow me on twitter @kunal2383 or may like the Facebook page of my blog http://www.facebook.com/blog.kunal2383.