如何访问联系人在Windows 10 UWP? [英] How to access Contacts in Windows 10 UWP?
问题描述
我想要访问联系人数据,所以我做了一些研究,并从MSDN找到了以下文章
I want to access Contacts Data and so I did some research and found the following article from MSDN
,我读取选择多个联系人部分,我使用,但每次,电子邮件和电话值为空。
From this article, I read selecting multiple contacts section and I used that but every time, the Emails and Phones value is null.
我的代码访问联系人如下:
My code to access contact is below:
var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
contactPicker.CommitButtonText = "Select";
contacts = await contactPicker.PickContactsAsync();
PhoneContactsList.Items.Clear();
if (contacts != null && contacts.Count > 0)
{
PhoneContactsList.Visibility = Windows.UI.Xaml.Visibility.Visible;
foreach (Contact contact in contacts)
{
ContactData eachContact = new ContactData();
eachContact.DisplayName = contact.DisplayName;
if (contact.Emails.Count > 0)
{
eachContact.EmailAddress = contact.Emails[0].Address;
}
else if (contact.Phones.Count > 0)
{
eachContact.PhoneNumber = contact.Phones[0].Number;
}
contactsData.Add(eachContact);
}
PhoneContactsList.ItemsSource = contactsData;
}
请建议我在这里做错了吗?
Please suggest what I am doing wrong here?
更新
当我调试代码并检查我看不到任何值, System._ComObject 。
When I debug the code and check I can't see any values, only thing shown is System._ComObject. I don't know what it means.
推荐答案
您需要按请求字段过滤联系人:
You need to filter contacts by requested fields:
var contactPicker = new ContactPicker();
contactPicker.CommitButtonText = "Select";
contactPicker.SelectionMode = ContactSelectionMode.Fields;
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email);
var contacts = await contactPicker.PickContactsAsync();
if (contacts != null && contacts.Count > 0)
{
foreach (Contact contact in contacts)
{
Debug.WriteLine(contact.DisplayName + contact.Emails[0].Address);
}
}
p.s。 System._ComObject是内部本地对象。您必须打开本机调试才能使用它。
p.s. System._ComObject is internal native object. You have to turn on native debugging to work with it.
这篇关于如何访问联系人在Windows 10 UWP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!