【发布时间】:2016-01-07 04:26:44
【问题描述】:
我想在 windows phone 10 中以编程方式实现功能编辑和添加联系人。
有可能吗?有没有关于它的样本?
【问题讨论】:
-
对于 Windows Phone 10,你的意思是?
-
是的,对于 windows phone 10,目前我只知道编辑由我的应用程序创建的联系人的方式,我想编辑我设备中存在的联系人
我想在 windows phone 10 中以编程方式实现功能编辑和添加联系人。
有可能吗?有没有关于它的样本?
【问题讨论】:
这是用于创建联系人的代码 sn-p:
public async Task AddContact(String FirstName, String LastName)
{
var contact = new Windows.ApplicationModel.Contacts.Contact();
contact.FirstName = FirstName;
contact.LastName = LastName;
//Here you can set other properties...
//Get he contact store for the app (so no lists from outlook and other stuff will be in the returned lists..)
var contactstore = await Windows.ApplicationModel.Contacts.ContactManager.RequestStoreAsync(Windows.ApplicationModel.Contacts.ContactStoreAccessType.AppContactsReadWrite);
try
{
var contactLists = await contactstore.FindContactListsAsync();
Windows.ApplicationModel.Contacts.ContactList contactList;
//if there is no contact list we create one
if (contactLists.Count == 0)
{
contactList = await contactstore.CreateContactListAsync("MyList");
}
//otherwise if there is one then we reuse it
else
{
contactList = contactLists.FirstOrDefault();
}
await contactList.SaveContactAsync(contact);
}
catch
{
//Handle it properly...
}
}
以下是更改现有联系人的简短示例:
//you can obviusly couple the changes better then this... this is just to show the basics
public async Task ChangeContact(Windows.ApplicationModel.Contacts.Contact ContactToChange, String NewFirstName, String NewLastName)
{
var contactStore = await Windows.ApplicationModel.Contacts.ContactManager.RequestStoreAsync(Windows.ApplicationModel.Contacts.ContactStoreAccessType.AppContactsReadWrite);
var contactList = await contactStore.GetContactListAsync(ContactToChange.ContactListId);
var contact = await contactList.GetContactAsync(ContactToChange.Id);
contact.FirstName = NewFirstName;
contact.LastName = NewLastName;
await contactList.SaveContactAsync(contact);
}
而且非常重要: 在 appxmanifest 中,您必须添加联系人功能。在解决方案资源管理器和“查看代码”中右键单击它,然后在功能下放置
<uap:Capability Name="contacts" />
没有用于此的用户界面。见this。
这两个示例都是作为起点......显然它还没有准备好生产,您必须根据您的场景对其进行调整。
由于这出现在 cmets 中,我稍微扩展了我的答案。
基于this(加上我自己的实验),聚合联系人的 ContactListId 为空(如果您考虑一下,这很有意义)。以下是如何使用 ContactlLstId 获取原始联系人(代码基于链接中的评论)
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}");
}
}
}
还有一件重要的事情:
根据documentation,您将无法更改其他应用程序创建的所有联系人。
AllContactsReadWrite:
对所有应用和系统联系人的读写权限。这个值是 不适用于所有应用程序。您的开发者帐户必须是专门的 由 Microsoft 提供,以便请求此级别的访问权限。
在某些情况下,当调用 SaveContactAsync(contact) 时,我会收到 System.UnauthorizedAccessException。一个例子是当联系人在 Skype 联系人列表中时。
【讨论】:
var contact = await contactList.GetContactAsync(ContactToChange.Id); 处崩溃。要获取联系人列表,我按照此链接中的步骤操作blogs.msdn.com/b/apendo/archive/2015/09/23/…
<Capabilities> <uap:Capability Name="contacts"/> </Capabilities> 添加到我的代码中)。我已阅读您有关“contactstoreaccesstype”的文档,因此如果我没有 Microsoft 专门提供,我无法编辑联系人属于另一个应用程序。