2017-05-05 18 views
0

我正在致力於維護聯繫人的應用程序,但未找到成功檢索我創建的聯繫人並對該聯繫人執行更新或刪除的聯繫人的方法。我已經熬它歸結爲一個簡單的例子: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更新 - 我不想創建第一個鏈接到第一個聯繫人當我保存更改時。 在此先感謝!

回答

1

這裏的問題是,從FindContactsAsync(String)方法返回的聯繫人是「聚合聯繫人」(即使他們沒有在People應用程序中鏈接)。由於它們不是您的應用程序創建的原始聯繫人,因此無法直接更改或刪除它們,這就是爲什麼在調用SaveContactAsyncDeleteContactAsync方法時遇到錯誤。

爲了解決這個問題,我們需要根據「聚合聯繫人」使用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 = "[email protected]"; //Change a value 

     await contactList.SaveContactAsync(rawContact); 
     //await contactList.DeleteContactAsync(rawContact); 
    } 
} 
+0

這樣做。謝謝! – Dan

相關問題