(Windows Phone 10)是否可以编辑,在Windows Phone 10中以编程方式添加新联系人?

我想实现函数编辑,并在Windows Phone 10中编程添加联系人。

可能吗? 有关于它的任何样品?

以下是创建联系人的代码片段:

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" /> 

这没有用户界面。 看到这个

这两个样本都是为了出发点…显然这不是生产准备,你必须适应你的情况。

更新

由于这是在评论中提出的,我稍微回答一下。

基于这个 (加上我自己的实验),聚合联系人的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}"); } } } 

还有一件重要的事情:

根据文档,您将无法更改所有由其他应用程序创建的联系人。

AllContactsReadWrite:

读写所有应用程序和系统联系人。 此值不适用于所有应用程序。 您的开发人员帐户必须由Microsoft专门设置才能请求此级别的访问权限。

在某些情况下,当SaveContactAsync(contact)被调用时,我得到一个System.UnauthorizedAccessException。 其中一个例子就是联系人在Skype联系人列表中。