如何通过电话号码联系?

时间:2016-01-22 18:09:59

标签: c# windows-10 uwp

如何使用C#通过电话号码联系(Windows.ApplicationModel.Contacts.Contact)?

2 个答案:

答案 0 :(得分: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}");

请务必在清单中添加通讯录功能。

答案 1 :(得分: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);
    }