Windows Phone 8 contacts people hub Address Book Management

Source: Internet
Author: User

Today, we will introduce people hub-networking APIs for Windows Phone 8, if you know about Windows Phone, you must know that you have read-only permission to operate the address book on the Windows phone7 API. You cannot directly add contacts to the address book. You can only synchronize contacts such as exchange, save deloge, this imposes a lot of user experience restrictions on some address book applications. However, the restriction opened in Microsoft's latest Windows Phone 8 SDK supports adding, deleting, modifying, and querying address books at the API level, next I will introduce it to you.

Of course writingCodeDo not forget to set the wmappmanifest File

1. Add a contact to the address book

First, we will introduce how to create a contact to the contact list. In SDK 8.0, contactstore is used for contact operations.

 
Contactstore store = await contactstore. createoropenasync (contactstoresystemaccessmode. readwrite, contactstoreapplicationaccessmode. readonly );

The createoropenasync method has two enumeration values contactstoresystemaccessmode and contactstoreapplicationaccessmode. The two enumeration values are more important. The former is that the contacts created by the current application can be edited and rewritten in people hub, the latter is declared in other applicationsProgramThe actual access permission in.

In People hub, readonly is read-only for system users.

 

Limitedreadonly can read all the information that can be seen by other applications, only the description information and the actual image readonly.

The code for adding a contact is very simple.

Async public void addcontact () {contactstore store = await contactstore. createoropenasync (contactstoresystemaccessmode. readwrite, contactstoreapplicationaccessmode. readonly); storedcontact contact = new storedcontact (store); // contact. remoteid = "123"; // contact. after the read-only property of ID is successfully added, the system will automatically allocate a 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 ");}

The remoteid is a identifier used to identify contacts. It can be used for data on the mapping service when the guid is modified synchronously, And the remoteid can be used to search for contacts in the application.

2. Modify the contact

Async private void updatecontact (string remoteid, string givenname, string familyname, string email, string codename) {contactstore = 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 ();}}

The process of modifying a contact is similar to that of creating a contact. You can use a remoteid or ID to obtain the target contact and modify and save the field to be modified.

3. delete a contact

Contactqueryresult result = store. createcontactquery (); ireadonlylist <storedcontact> contacts = await result. getcontactsasync (); List <storedcontact> listsc = contacts. tolist (); await store. deletecontactasync (listsc [0]. ID );

To delete a contact, you only need the storedcontact ID to delete the contact.

4. query contacts

Query of contacts still supports 7.5 of query methods (supports filtering conditions and supports LINQ)

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 ());}

Http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh286416 (V = vs.105). aspx

Filter condition: http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh286417 (V = vs.105). aspx

In addition

Createcontactquery

Async private void querywithdesiredfields () {contactstore store = await contactstore. createoropenasync (); contactqueryoptions = new contactqueryoptions (); options. desiredfields. add (knowncontactproperties. email); options. orderby = contactqueryresultordering. familynamegivenname; contactqueryresult result = store. createcontactquery (options); ireadonlylist <storedcontact> contacts = await result. getcontactsasync ();}

After the application is added to the people hub, the contact display can also be filtered.

Enter contacts-settings-filter my contacts list-select contacts from the application to be displayed

 

Selected by default

Http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207024 (V = vs.105). aspx

You are welcome to communicate with me here or on Sina Weibo.@ Wang Bo _ Nick

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.