Windows Phone 8 將允許開發人員建立自己的Custom Contact Store。應用程式通過應用程式添加新連絡人後,先連絡人出現在Windows Phone hub中,並和使用者的系統連絡人並存。開發人員可以為建立連絡人的的標準屬性,如電話號碼、名稱等,也可以儲存自訂屬性。開發人員還可以利用連絡人儲存api來同步他們的使用者的連絡人清單到雲端。
建立連絡人:
private async void Button_Click_4(object sender, RoutedEventArgs e) { //var store = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadOnly, // ContactStoreApplicationAccessMode.LimitedReadOnly); //連結並開啟連絡人, 需要添加<Capability Name="ID_CAP_CONTACTS" /> 能力 var store = await ContactStore.CreateOrOpenAsync(); Debug.WriteLine("RevisionNumber:" + store.RevisionNumber); StoredContact sc = new StoredContact(store); sc.DisplayName = "Mark"; sc.HonorificPrefix = "et"; await sc.SaveAsync(); }
讀取連絡人
private async void Button_Click_2(object sender, RoutedEventArgs e) { //var store = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadOnly, // ContactStoreApplicationAccessMode.LimitedReadOnly); //連結並開啟連絡人, 需要添加<Capability Name="ID_CAP_CONTACTS" /> 能力 var store = await ContactStore.CreateOrOpenAsync(); Debug.WriteLine("RevisionNumber:" + store.RevisionNumber); //建立連絡人查詢 ContactQueryResult result = store.CreateContactQuery(); var count = await result.GetContactCountAsync(); Debug.WriteLine("GetContactCountAsync:" + count); ContactQueryOptions option = result.GetCurrentQueryOptions(); foreach (string filed in option.DesiredFields) { Debug.WriteLine("filed:" + filed); } //擷取連絡人清單 var contacts = await result.GetContactsAsync(); foreach (StoredContact contact in contacts) { Debug.WriteLine("DisplayName:" + contact.DisplayName); //將連絡人資料轉換成VCard檔案 var vCard = await contact.ToVcardAsync(); IInputStream inputStream = vCard.GetInputStreamAt(0); ulong length = vCard.Size; try { //將vCard檔案流使用字串輸出 var readBuf = new Windows.Storage.Streams.Buffer((uint)length); var vCardOp = vCard.GetInputStreamAt(0).ReadAsync(readBuf, (uint)length, InputStreamOptions.Partial); vCardOp.Completed = (IAsyncOperationWithProgress<IBuffer, uint> asyncAction, AsyncStatus asyncStatus) => { switch (asyncStatus) { case AsyncStatus.Completed: Debug.WriteLine("vCardString:" + MainPage.BufferToString(readBuf)); break; case AsyncStatus.Error: break; case AsyncStatus.Canceled: // Read is not cancelled in this sample. break; } }; } catch (Exception exp) { Debug.WriteLine(exp.ToString()); } } }