The problem I want to solve:
Get all the contacts info like name and Mobile phone and write it into file and save in ISO.
- How to use SearchAsync if I want to search available contacts in the phone?
- How to iterate the return-results and write to file one by one of the contact into a file?
Here’s the code I have:
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
Contacts contacts = new Contacts();
contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
contacts.SearchAsync(displayName,FilterKind.DisplayName,null);
//search for all contacts
contacts.SearchAsync(string.Empty, FilterKind.None, null);
}
Update:
-
The below code throw NullException error if the PhoneNumber is Empty. Why?
-
How to get all the possibile phone number other than
result.PhoneNumbers.FirstOrDefault().ToString(); -
Same question for EmailAddresses
Using this to search all contacts in the phone:
contacts.SearchAsync(searchterm, FilterKind.None, null);
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
int intTTL = e.Results.Count();
if (intTTL != 0)
{
MessageBox.Show(intTTL.ToString());
foreach (var result in e.Results)
{
string strTTL;
string strName = result.DisplayName;
string MobileNo = result.PhoneNumbers.FirstOrDefault().ToString();
strTTL = strName + "," + MobileNo;
MessageBox.Show(strTTL);
}
else
{
MessageBox.Show("You have not entered any contact info at all.");
}
}
Have a look at this article to see how to get the Contacts:
To iterate:
See this MSDN page for writing files in isolated storage