2016-01-22 162 views

回答

2

您將需要爲此與ContactStore.FindContactsAsync聯繫,然後使用linq解析它們。正如在MSDN上所說的,您可以通過傳遞一個數字來搜索聯繫人,但正如我所嘗試的那樣,您必須輸入整個數字。所以,如果你想搜索例如包含「199」的所有聯繫人,你應該能夠做到這一點是這樣的:

var contactStore = await ContactManager.RequestStoreAsync(); 
var contacts = await contactStore.FindContactsAsync(); 
var contactsByNumber = contacts.Where(x => x.Phones.Any(y => y.Number.Contains("199"))); 
//var contactsByNumber = await contactStore.FindContactsAsync("112"); // you may search like this but you must enter full number 
foreach (var item in contactsByNumber) Debug.WriteLine($"Name: {item.DisplayName} Phone: {item.Phones.FirstOrDefault().Number}"); 

記住添加聯繫人能力清單中。

2

使用此:)

private async Task<Contact> SearchContactByPhoneNumberAsync(string phoneNumber) 
{ 
    Contact resultContact = null; 
    var contactStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly); 
    var contactReader = contactStore.GetContactReader(new ContactQueryOptions(phoneNumber)); 
    while (true) 
    { 
     var contactBatch = await contactReader.ReadBatchAsync(); 
     if (contactBatch.Contacts.Count > 0) 
     { 
      foreach (var contact in contactBatch.Contacts) 
      { 
       if (contact.Phones?.Count > 0) 
       { 
        foreach (var phone in contact.Phones) 
        { 
         if (phone.Number == phoneNumber) 
         { 
          resultContact = contact; 
          break; 
         } 
        } 
       } 
      } 
     } 
     else 
     { 
      break; 
     } 
    } 

    return resultContact; 
} 

我已經創建了數0635806354測試接觸和使用代碼波紋管搜索:

var result = await SearchContactByPhoneNumberAsync("+38 (063) 580-63-54"); 
    if (result != null) 
    { 
     Debug.WriteLine(result.Name); 
    }