获取 Windows 10 Phone 上的联系人列表

Get the list of Contacts on Windows 10 Phone

我需要知道如何阅读 Windows 10 Phone 上的联系人列表。我不想使用联系人选择器;我只需要能够遍历所有联系人以访问他们的姓名和 phone 号码,并将其存储在列表中。 (类似于 WhatsApp 能够读取您的联系人列表并将其显示在他们的应用程序中的方式)

我从另一个答案中提取了一些代码

使用此代码,您应该能够获取联系人。

public async Task IterateThroughContactsForContactListId()
 {            
            ContactStore allAccessStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);

            var contacts = await allAccessStore.FindContactsAsync();
            foreach (var contact in contacts)
            {
                //process aggregated contacts
                if (contact.IsAggregate)
                {
                    //here contact.ContactListId is "" (null....)                  
                    //in this case if you need the the ContactListId then you need to iterate through the raw contacts
                    var rawContacts = await allAccessStore.AggregateContactManager.FindRawContactsAsync(contact);
                    foreach (var rawContact in rawContacts)
                    {
                        //Here you should have ContactListId
                        Debug.WriteLine($"aggregated, name: {rawContact.DisplayName }, ContactListId: {rawContact.ContactListId}");
                    }
                }
                else //not aggregated contacts should work
                {
                    Debug.WriteLine($"not aggregated, name: {contact.DisplayName }, ContactListId: {contact.ContactListId}");
                }
            }

}

他还指出:

And very important: In the appxmanifest you have to add the contacts capability. Right click to it in the solution explorer and "View Code" and then under Capabilities put

<uap:Capability Name="contacts" />

There is no UI for this. See this.