WP8 电话本编程

来源:互联网 发布:徐州网络 编辑:程序博客网 时间:2024/06/03 21:23
通讯录搜索: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类的对象。

这两个类的具体用法,请查看帮助。


只读联系人访问

private void Button_Click_ReadContact(object sender, RoutedEventArgs e){    Contacts cons = new Contacts();    cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);    // 参数1: 关键字;参数2:过滤类型,包含显示名称、电话号码、邮件地址、是否固定到开始屏幕等    cons.SearchAsync("139", FilterKind.PhoneNumber, "状态");}
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e){    //获取到的联系人的集合    listbox1.ItemsSource = e.Results;}



(1) 设置工程属性中的 WMAppManifest 文件
钩选 ID_CAP_CONTACTS 项以支持电话本操作


(2) 在 WP SDK 8.0 中 ContactStore 用于联系人操作


(3) 增加联系人
新增程序联系人需要先创建或者打开程序的联系人存储ContactStore,并且可以设置该程序联系人存储的被访问权限。

第一种方法:直接通过联系人存储来创建

async Task Add(string familyName, string givenName){    ContactStore store = await ContactStore.CreateOrOpenAsync();    StoredContact contact = new StoredContact(store);    contact.FamilyName = familyName;    contact.GivenName = givenName;    // 获取已知联系人属性    IDictionary<string, object> props = await contact.GetPropertiesAsync();    props.Add(KnownContactProperties.Telephone, "18600000000");    // 获取扩展的联系人属性    IDictionary<string, object> extprops = await contact.GetExtendedPropertiesAsync();    extprops.Add("扩展", "扩展属性");    await contact.SaveAsync();}

async public void AddContact()  
{  
    ContactStore store = await ContactStore.CreateOrOpenAsync(  
                        ContactStoreSystemAccessMode.ReadWrite,  
                        ContactStoreApplicationAccessMode.ReadOnly);  
    StoredContact contact = new StoredContact(store);  
    //  contact.RemoteId = "123";  
    //  contact.Id               // 只读属性添加成功后系统会自动分配  
    contact.GivenName = txtGivenName.Text;  
    contact.FamilyName = txtFamilyName.Text;  
    IDictionary<string, object> props = await contact.GetPropertiesAsync();  
    props.Add(KnownContactProperties.Email, txtMail.Text);  
    props.Add(KnownContactProperties.Telephone, txtPhone.Text);  
    await contact.SaveAsync();  
    MessageBox.Show("save done");  

第二种方式:通过ContactInformation类对象创建联系人
// 创建一个ContactInformation类
ContactInformation conInfo = new ContactInformation();
//  获取ContactInformation类的属性map表
var properties = await conInfo.GetPropertiesAsync();
//  添加电话属性
properties.Add(KnownContactProperties.Telephone, "123456");
// 添加名字属性
properties.Add(KnownContactProperties.GivenName, "名字");
// 创建或者打开联系人存储
ContactStore conStore = await ContactStore.CreateOrOpenAsync();
// 保存联系人
StoredContact storedContact = new StoredContact(conStore, conInfo);
// 保存联系人
await storedContact.SaveAsync();


(4) 修改联系人
async private void UpdateContact(string remoteId, string givenName, string familyName, string email, string codeName)  
{  
    ContactStore store = await ContactStore.CreateOrOpenAsync();  
    StoredContact contact = await store.FindContactByRemoteIdAsync(remoteId);  


    if (contact != null)  
    {  
        contact.GivenName = givenName;  
        contact.FamilyName = familyName;  


        IDictionary<string, object> props = await contact.GetPropertiesAsync();  
        props[KnownContactProperties.Email] = email;  


        IDictionary<string, object> extprops = await contact.GetExtendedPropertiesAsync();  
        extprops["Codename"] = codeName;  


        await contact.SaveAsync();  
    }  



(5) 删除联系人
ContactQueryResult result = store.CreateContactQuery();  
IReadOnlyList<StoredContact> contacts = await result.GetContactsAsync();  
List<StoredContact> listSC = contacts.ToList();  
await store.DeleteContactAsync(listSC[0].Id);  


(6) 查询联系人
private void ButtonContacts_Click(object sender, RoutedEventArgs e)  
{  
    Contacts cons = new Contacts();  
 
    //Identify the method that runs after the asynchronous search completes.  
    cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);  
 
    //Start the asynchronous search.  
    cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");  
}  
 
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)  
{  
    //Do something with the results.  
    MessageBox.Show(e.Results.Count().ToString());  

联系人查询也需要创建联系人存储,创建联系人存储的形式和联系人新增是一样的。联系人查询是通过ContactStore的CreateContactQuery方法来创建一个查询,可以查询的参数ContactQueryOptions来设置查询返回的结果和排序的规则,创建的查询时ContactQueryResult类型。可以通过ContactQueryResult类的GetContactsAsync异步方法获取联系人存储中的联系人列表和通过GetCurrentQueryOptions方法获取当前的查询条件。
conStore = await ContactStore.CreateOrOpenAsync();
ContactQueryResult conQueryResult = conStore.CreateContactQuery();
uint count = await conQueryResult.GetContactCountAsync();

IReadOnlyList<StoredContact> conList = await conQueryResult.GetContactsAsync();


// 查询联系人,并绑定到列表async void Query(){    ContactStore store = await ContactStore.CreateOrOpenAsync();    ContactQueryResult result = store.CreateContactQuery();    IReadOnlyList<StoredContact> contacts = await result.GetContactsAsync();    listbox1.ItemsSource = contacts;}// 查询已知属性async void GetPropertie(StoredContact contact){    IDictionary<string, object> props = await contact.GetPropertiesAsync();    if (props.Keys.Contains(KnownContactProperties.Telephone))    {        MessageBox.Show("手机:" + props[KnownContactProperties.Telephone].ToString());    }}// 更新联系人async Task Update(string Id){    try    {        ContactStore store = await ContactStore.CreateOrOpenAsync();        var contact = await store.FindContactByIdAsync(Id);        if (contact != null)        {            contact.FamilyName = "某";            contact.GivenName = "人";            await contact.SaveAsync();        }    }    catch (Exception e)    {        textblock1.Text = e.Message;    }}// 删除联系人async private Task Delete(string Id){    try    {        ContactStore store = await ContactStore.CreateOrOpenAsync();        await store.DeleteContactAsync(Id);    }    catch (Exception e)    {        textblock1.Text = e.Message;    }}// 新增private async void Button_Click_1(object sender, RoutedEventArgs e){    string[] familyNames = new string[] { "郑", "钱", "郭", "罗", "贺" };    string[] givenNames = new string[] {"家","航","燕","白","公" };    Random r = new Random();    await Add(familyNames[r.Next(0, familyNames.Length)], givenNames[r.Next(0, givenNames.Length)]);    Query();}// 修改private async void Button_Click_2(object sender, RoutedEventArgs e){   var contact= listbox1.SelectedValue as StoredContact;   if(null == contact) return;   await Update(contact.Id);   Query();}// 删除private async void Button_Click_3(object sender, RoutedEventArgs e){    var contact = listbox1.SelectedValue as StoredContact;    if(null == contact) return;    await Delete(contact.Id);    Query();}// 查看手机号private void Button_Click_4(object sender, RoutedEventArgs e){    var contact = listbox1.SelectedValue as StoredContact;    if(null == contact) return;    GetPropertie(contact);}


0 0