C# 如何从Windows Phone应用程序中的联系人获取所有数据?
我想要所有联系人的姓名和照片,并将其存储在某个地方?我们怎么能做到呢?据我所知,它只允许搜索一个并获取其详细信息。您应该从msdn中的阅读开始。有关代码段,请选中 例如,msdn中用于计算这些项目的样本:C# 如何从Windows Phone应用程序中的联系人获取所有数据?,c#,xaml,windows-phone-8,windows-phone,C#,Xaml,Windows Phone 8,Windows Phone,我想要所有联系人的姓名和照片,并将其存储在某个地方?我们怎么能做到呢?据我所知,它只允许搜索一个并获取其详细信息。您应该从msdn中的阅读开始。有关代码段,请选中 例如,msdn中用于计算这些项目的样本: private void ButtonContacts_Click(object sender, RoutedEventArgs e) { Contacts cons = new Contacts(); //Identify the method that runs afte
private void ButtonContacts_Click(object sender, RoutedEventArgs e)
{
Contacts cons = new Contacts();
//Identify the method that runs after the asynchronous search completes.
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
//Start the asynchronous search.
cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
}
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
//Do something with the results.
MessageBox.Show(e.Results.Count().ToString());
}
更新2:
例如,如果您想要姓名、第一封邮件和第一个号码,可以使用如下代码:
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
IEnumerable<Contact> contacts = e.Results; //Here your result
string everynames = String.Empty;
foreach (var item in contacts)
{
//We can get attributes from each item
everynames += item.DisplayName + ";" //Get name
+ (item.EmailAddresses.Count() > 0 ? (item.EmailAddresses.FirstOrDefault()).EmailAddress : "") + ";" //Check if contact has an email. If so, display it. He can be more than one !
+ (item.PhoneNumbers.Count() > 0 ? (item.PhoneNumbers.FirstOrDefault()).PhoneNumber : "") + ";" //Check if contact has a phonenumber. If so, display it. He can be more than one !
+ Environment.NewLine;
}
MessageBox.Show(everynames);
}
别忘了试着抓住{}
您可以在
SearchAsync()
中更改FilterKind
。我们使用FilterKind.None
获取所有信息。您可以使用Microsoft.Phone.UserData
名称空间访问WindowsPhone上的联系人数据,下面是一篇关于实现这一点的完整文章,但是如果您想在创建联系人方面走得更远,请尝试使用该类,它有许多方法可以帮助您完成所需的任务
更新:
如果要获取所有联系人:
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
IEnumerable<Contact> contacts = e.Results; //Here your result
string everynames = String.Empty;
foreach (var item in contacts)
{
//We can get attributes from each item
everynames += item.DisplayName + Environment.NewLine;
}
MessageBox.Show(everynames);
}
代码>
private void ButtonContacts_Click(object sender, RoutedEventArgs e)
{
Contacts cons = new Contacts();
//Identify the method that runs after the asynchronous search completes.
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
//Start the asynchronous search.
cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
}
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
/* Here use the e.Results to return an object of type QueryDataEnumerable<Microsoft.Phone.UserData.Contact> where you can enumerate through the contacts returned*/
}
我读过msdn链接,但给出的示例是搜索联系人然后获取其详细信息,实际上我想访问所有联系人详细信息并单独保存。我更新了我的答案,这就是你要找的吗?我通过循环获得了姓名,你能告诉我如何获取图片吗?再次非常感谢@AymenDaudi我已经把这些放在方法中,var contacts=e.结果//这里是结果字符串everynames=string.Empty;foreach(contacts中的var item){Stream s=((Contact)item.First()).GetPicture();//我们可以从每个项获取属性},但它表示未找到流命名空间,并且System.Collections.Generic.IEnumerable未包含在First的定义中。基本上,我需要使用System.IO获取每个联系人的图片;然后将
(Contact)item.First()
替换为item
实际上我读过这个示例,但它没有说明如何一次获取所有数据。这里有一个函数,允许这样做?你能给我一个使用这个的示例代码吗?在Contacts\u SearchCompleted()
中,在MessageBox
前面添加这一行:var Contacts=e.Results代码>。接触是接触的数不清的。您可以使用它访问您想要的内容。如果您愿意,我添加了另一个示例。@user3235018:ContactQueryResult.GetContactsAsync()您尝试对ContactStore对象使用works,它在这里对您没有帮助,我将在我的回答中添加它作为更新。ContactStore对象可能很有用。它是具体的。我现在真的不知道@user3235018想对这些数据做什么。如果他想重建像本机控件“people”这样的控件,他应该检查ContactStore对象。
private void ButtonContacts_Click(object sender, RoutedEventArgs e)
{
Contacts cons = new Contacts();
//Identify the method that runs after the asynchronous search completes.
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
//Start the asynchronous search.
cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
}
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
/* Here use the e.Results to return an object of type QueryDataEnumerable<Microsoft.Phone.UserData.Contact> where you can enumerate through the contacts returned*/
}
Stream s = ((Contact)e.Results.First()).GetPicture();