标签:
通讯录搜索:http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh286416(v=vs.105).aspx
过滤条件:http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh286417(v=vs.105).aspx
程序联系人存储的API在空间Windows.Phone.PersonalInformation下,下面来看一下如何去使用这些API来操作联系人。
ContactStore类和StoredContact类
ContactStore类表示一个Windows Phone应用程序的自定义联系人存储,它是应用程序存储的一个管理者,负责管理应用程序所创建的联系人。
StoredContact类表示一个应用程序自定义的联系人存储,它继承了IContactInformation接口,所有由应用程序创建的联系人都是一个StoredContact类的对象。
这两个类的具体用法,请查看帮助。
只读联系人访问
1 private void Button_Click_ReadContact(object sender, RoutedEventArgs e) 2 { 3 Contacts cons = new Contacts(); 4 cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted); 5 // 参数1: 关键字;参数2:过滤类型,包含显示名称、电话号码、邮件地址、是否固定到开始屏幕等 6 cons.SearchAsync("139", FilterKind.PhoneNumber, "状态"); 7 }
1 void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e) 2 { 3 //获取到的联系人的集合 4 listbox1.ItemsSource = e.Results; 5 }
(1) 设置工程属性中的 WMAppManifest 文件
钩选 ID_CAP_CONTACTS 项以支持电话本操作
(2) 在 WP SDK 8.0 中 ContactStore 用于联系人操作
(3) 增加联系人
新增程序联系人需要先创建或者打开程序的联系人存储ContactStore,并且可以设置该程序联系人存储的被访问权限。
第一种方法:直接通过联系人存储来创建
1 async Task Add(string familyName, string givenName) 2 { 3 ContactStore store = await ContactStore.CreateOrOpenAsync(); 4 StoredContact contact = new StoredContact(store); 5 contact.FamilyName = familyName; 6 contact.GivenName = givenName; 7 8 // 获取已知联系人属性 9 IDictionary<string, object> props = await contact.GetPropertiesAsync(); 10 props.Add(KnownContactProperties.Telephone, "18600000000"); 11 12 // 获取扩展的联系人属性 13 IDictionary<string, object> extprops = await contact.GetExtendedPropertiesAsync(); 14 extprops.Add("扩展", "扩展属性"); 15 16 await contact.SaveAsync(); 17 }
1 async public void AddContact() 2 { 3 ContactStore store = await ContactStore.CreateOrOpenAsync( 4 ContactStoreSystemAccessMode.ReadWrite, 5 ContactStoreApplicationAccessMode.ReadOnly); 6 StoredContact contact = new StoredContact(store); 7 // contact.RemoteId = "123"; 8 // contact.Id // 只读属性添加成功后系统会自动分配 9 contact.GivenName = txtGivenName.Text; 10 contact.FamilyName = txtFamilyName.Text; 11 IDictionary<string, object> props = await contact.GetPropertiesAsync(); 12 props.Add(KnownContactProperties.Email, txtMail.Text); 13 props.Add(KnownContactProperties.Telephone, txtPhone.Text); 14 await contact.SaveAsync(); 15 MessageBox.Show("save done"); 16 }
第二种方式:通过ContactInformation类对象创建联系人
1 // 创建一个ContactInformation类 2 ContactInformation conInfo = new ContactInformation(); 3 // 获取ContactInformation类的属性map表 4 var properties = await conInfo.GetPropertiesAsync(); 5 // 添加电话属性 6 properties.Add(KnownContactProperties.Telephone, "123456"); 7 // 添加名字属性 8 properties.Add(KnownContactProperties.GivenName, "名字"); 9 // 创建或者打开联系人存储 10 ContactStore conStore = await ContactStore.CreateOrOpenAsync(); 11 // 保存联系人 12 StoredContact storedContact = new StoredContact(conStore, conInfo); 13 // 保存联系人 14 await storedContact.SaveAsync();
(4) 修改联系人
1 async private void UpdateContact(string remoteId, string givenName, string familyName, string email, string codeName) 2 { 3 ContactStore store = await ContactStore.CreateOrOpenAsync(); 4 StoredContact contact = await store.FindContactByRemoteIdAsync(remoteId); 5 6 7 if (contact != null) 8 { 9 contact.GivenName = givenName; 10 contact.FamilyName = familyName; 11 12 13 IDictionary<string, object> props = await contact.GetPropertiesAsync(); 14 props[KnownContactProperties.Email] = email; 15 16 17 IDictionary<string, object> extprops = await contact.GetExtendedPropertiesAsync(); 18 extprops["Codename"] = codeName; 19 20 21 await contact.SaveAsync(); 22 } 23 }
(5) 删除联系人
1 ContactQueryResult result = store.CreateContactQuery(); 2 IReadOnlyList<StoredContact> contacts = await result.GetContactsAsync(); 3 List<StoredContact> listSC = contacts.ToList(); 4 await store.DeleteContactAsync(listSC[0].Id);
(6) 查询联系人
1 private void ButtonContacts_Click(object sender, RoutedEventArgs e) 2 { 3 Contacts cons = new Contacts(); 4 5 //Identify the method that runs after the asynchronous search completes. 6 cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted); 7 8 //Start the asynchronous search. 9 cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1"); 10 } 11 12 void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e) 13 { 14 //Do something with the results. 15 MessageBox.Show(e.Results.Count().ToString()); 16 }
联系人查询也需要创建联系人存储,创建联系人存储的形式和联系人新增是一样的。联系人查询是通过ContactStore的CreateContactQuery方法来创建一个查询,可以查询的参数ContactQueryOptions来设置查询返回的结果和排序的规则,创建的查询时ContactQueryResult类型。可以通过ContactQueryResult类的GetContactsAsync异步方法获取联系人存储中的联系人列表和通过GetCurrentQueryOptions方法获取当前的查询条件。
1 conStore = await ContactStore.CreateOrOpenAsync(); 2 ContactQueryResult conQueryResult = conStore.CreateContactQuery(); 3 uint count = await conQueryResult.GetContactCountAsync(); 4 IReadOnlyList<StoredContact> conList = await conQueryResult.GetContactsAsync();
1 // 查询联系人,并绑定到列表 2 async void Query() 3 { 4 ContactStore store = await ContactStore.CreateOrOpenAsync(); 5 ContactQueryResult result = store.CreateContactQuery(); 6 IReadOnlyList<StoredContact> contacts = await result.GetContactsAsync(); 7 8 listbox1.ItemsSource = contacts; 9 } 10 11 // 查询已知属性 12 async void GetPropertie(StoredContact contact) 13 { 14 IDictionary<string, object> props = await contact.GetPropertiesAsync(); 15 if (props.Keys.Contains(KnownContactProperties.Telephone)) 16 { 17 MessageBox.Show("手机:" + props[KnownContactProperties.Telephone].ToString()); 18 } 19 } 20 21 // 更新联系人 22 async Task Update(string Id) 23 { 24 try 25 { 26 ContactStore store = await ContactStore.CreateOrOpenAsync(); 27 var contact = await store.FindContactByIdAsync(Id); 28 if (contact != null) 29 { 30 contact.FamilyName = "某"; 31 contact.GivenName = "人"; 32 await contact.SaveAsync(); 33 } 34 } 35 catch (Exception e) 36 { 37 textblock1.Text = e.Message; 38 } 39 } 40 41 // 删除联系人 42 async private Task Delete(string Id) 43 { 44 try 45 { 46 ContactStore store = await ContactStore.CreateOrOpenAsync(); 47 await store.DeleteContactAsync(Id); 48 } 49 catch (Exception e) 50 { 51 textblock1.Text = e.Message; 52 } 53 } 54 55 // 新增 56 private async void Button_Click_1(object sender, RoutedEventArgs e) 57 { 58 string[] familyNames = new string[] { "郑", "钱", "郭", "罗", "贺" }; 59 string[] givenNames = new string[] {"家","航","燕","白","公" }; 60 Random r = new Random(); 61 62 await Add(familyNames[r.Next(0, familyNames.Length)], givenNames[r.Next(0, givenNames.Length)]); 63 Query(); 64 } 65 // 修改 66 private async void Button_Click_2(object sender, RoutedEventArgs e) 67 { 68 69 var contact= listbox1.SelectedValue as StoredContact; 70 if(null == contact) return; 71 await Update(contact.Id); 72 Query(); 73 } 74 75 // 删除 76 private async void Button_Click_3(object sender, RoutedEventArgs e) 77 { 78 var contact = listbox1.SelectedValue as StoredContact; 79 if(null == contact) return; 80 await Delete(contact.Id); 81 Query(); 82 } 83 84 // 查看手机号 85 private void Button_Click_4(object sender, RoutedEventArgs e) 86 { 87 var contact = listbox1.SelectedValue as StoredContact; 88 if(null == contact) return; 89 GetPropertie(contact); 90 }
标签:
原文地址:http://www.cnblogs.com/91program/p/5205132.html