我正在开发一个应用程序以维护联系人,但我还没有找到一种方法来成功检索我创建的联系人并对该联系人执行更新或删除。我把它归结为一个简单的例子:
public async Task TestContact()
{
try
{
//Make a list
ContactStore store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite);
var lists = await store.FindContactListsAsync();
ContactList list = lists.FirstOrDefault((x) => x.DisplayName == "Test List");
if (list == null)
{
list = await store.CreateContactListAsync("Test List");
list.OtherAppReadAccess = ContactListOtherAppReadAccess.Full;
list.OtherAppWriteAccess = ContactListOtherAppWriteAccess.SystemOnly;
await list.SaveAsync();
}
//Make a contact and save it
Contact contact = new Contact();
contact.FirstName = "Test";
contact.LastName = "Contact";
await list.SaveContactAsync(contact);
//Modify the existing one just to show that saving again works
ContactEmail email = new ContactEmail();
email.Address = "test@test.com";
contact.Emails.Add(email);
await list.SaveContactAsync(contact); //This line updates existing contact as desired.
//Now simulate finding that contact, modify it, and save it
store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite);
var contacts = await store.FindContactsAsync("Test Contact");
contact = contacts[0];
contact.Emails[0].Address = "newemail@test.com"; //Change a value
lists = await store.FindContactListsAsync();
list = lists.FirstOrDefault((x) => x.DisplayName == "Test List");
if (list != null)
{
await list.SaveContactAsync(contact); //This line throws "The operation identifier is not valid"
await list.DeleteContactAsync(contact); //This line throws "Value does not fall within the expected range"
}
}
catch (Exception ex)
{
//exception thrown!
}
}
代码根据需要创建新列表,向其添加联系人,并更新该联系人。然后,它会尝试通过搜索并调用save(或delete)来检索该联系人。保存和删除抛出异常,如评论中所述。
有没有人能够在通过搜索找到联系人后更新联系人?最终,我真的希望能够更新联系人。我只是因为无法保存而尝试删除(UPDATE = DELETE + ADD)
请注意,我想要一个IN PLACE更新 - 我对创建第二个联系人不感兴趣,该联系人在保存更改时链接到第一个联系人。 提前谢谢!
答案 0 :(得分:1)
这里的问题是从FindContactsAsync(String)方法返回的联系人是“聚合联系人”(即使他们没有在人员应用中链接)。由于它们不是您的应用创建的原始联系人,因此您无法直接更改或删除它们,这就是您在调用SaveContactAsync或DeleteContactAsync方法时出错的原因。
要解决此问题,我们需要根据“聚合联系人”使用FindRawContactsAsync(Contact)方法获取原始联系人,然后修改,保存或删除原始联系人。例如:
//Now simulate finding that contact, modify it, and save it
var store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite);
var contacts = await store.FindContactsAsync("Test Contact");
//This contact is a "aggregate contact"
var contact = contacts[0];
//Get the raw contacts according to the "aggregate contact"
var allStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);
var rawContacts = await allStore.AggregateContactManager.FindRawContactsAsync(contact);
foreach (var rawContact in rawContacts)
{
var contactList = await store.GetContactListAsync(rawContact.ContactListId);
if (contactList != null && contactList.DisplayName == "Test List")
{
rawContact.Emails[0].Address = "newemail@test.com"; //Change a value
await contactList.SaveContactAsync(rawContact);
//await contactList.DeleteContactAsync(rawContact);
}
}