Windows Phone SDK 7.1 provides an API for obtaining contact list information. Although it is read-only, it is quite good than SDK 7.0. The related APIs for obtaining Contact information are located in the Microsoft. Phone. UserData space. Two main types are used for obtaining Contact information: Contacts and Contact. The following are examples of getting contact information and precautions.
1. First, create a Contacts object and query the contact information asynchronously. Therefore, you need to add a query completion event (SearchCompleted)
Contacts contacts = new Contacts ();
Contacts. SearchCompleted + = new EventHandler <ContactsSearchEventArgs> (SearchCompleted );
2. Call query to start searching for contacts.
Contacts. SearchAsync (String. Empty, FilterKind. DisplayName, null );
SearchAsync (stringfilter, FilterKind filterKind, Object state) asynchronously searches for contact information in the user's contact data. filter is the filter, FilterKind filter type, and all contact information is queried in the above column.
3. After the query is complete, the query completion event (SearchCompleted) will be called)
Private void SearchCompleted (object sender, ContactsSearchEventArgs e)
{
Foreach (Contact result in e. Results)
{
// Contact name
String name = Contact. DisplayName;
// Contact phone number
ContactPhoneNumber phoneNumber = Contact. PhoneNumbers. FirstOrDefault ();
String phone = String. Empty;
If (phoneNumber! = Null)
{
Phone = phoneNumber. PhoneNumber;
}
// Contact address
ContactAddress address = Contact. Addresses. FirstOrDefault ();
String addr = String. Empty;
If (address! = Null)
{
Addr = address. PhysicalAddress. AddressLine1;
}
// Contact Email
ContactEmailAddress emailAddress = Contact. EmailAddresses. FirstOrDefault ();
String email = String. Empty;
If (emailAddress! = Null)
{
Email = emailAddress. EmailAddress;
}
//............
}
}
Note 1: If FirstOrDefault () is a function of the System. Linq. Enumerable class, you must manually add the namespace using System. Linq;
NOTE 2: To obtain the contact address PhysicalAddress. AddressLine1, you must add the reference System. Device.