我正在致力於維護聯繫人的應用程序,但未找到成功檢索我創建的聯繫人並對該聯繫人執行更新或刪除的聯繫人的方法。我已經熬它歸結爲一個簡單的例子:UWP - 更新或刪除現有聯繫人時引發的異常
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 = "[email protected]";
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 = "[email protected]"; //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)來檢索該聯繫人。如註釋中所述,保存和刪除拋出異常。
有沒有人能夠通過搜索找到它後更新聯繫人?最終,我真的希望能夠更新聯繫方式。我只是試圖刪除,因爲無法保存(更新=刪除+添加)
請注意,我想要一個IN PLACE更新 - 我不想創建第一個鏈接到第一個聯繫人當我保存更改時。 在此先感謝!
這樣做。謝謝! – Dan