UWP - 更新或删除现有联系人时抛出异常

UWP - Exceptions thrown when Updating Or Deleting existing contact

我正在开发一个用于维护联系人的应用程序,但我还没有找到一种方法来成功检索我创建的联系人并对该联系人执行更新或删除。我把它归结为一个简单的例子:

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!
    }
}

该代码根据需要创建一个新列表,向其中添加一个联系人,并就地更新该联系人。然后它会尝试通过搜索并调用保存(或删除)来检索该联系人。如评论中所述,保存和删除都会抛出异常。

有没有人能够在通过搜索找到联系人后更新联系人?最终,我真的希望能够就地更新联系人。我只是尝试删除,因为无法保存 (UPDATE = DELETE + ADD)

请注意,我想要 IN PLACE 更新 - 我对在保存更改时创建链接到第一个联系人的第二个联系人不感兴趣。 提前致谢!

这里的问题是联系人从 FindContactsAsync(String) method are "aggregate contacts" (even though they are not linked in People app). As they are not the raw contacts created by your app, you are not able to change or delete them directly and that's why you got errors when calling SaveContactAsync or DeleteContactAsync 方法返回。

为了解决这个问题,我们需要根据"aggregate contact"使用FindRawContactsAsync(Contact)方法获取raw contact,然后修改,保存或删除raw 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);
    }
}