In this blog, http://blog.csdn.net/tcjiaan.
We have discussed how to use the file selector to select a file. In fact, there is also a directory selector. However, let me leave it alone because it is a truth to use and select a file, let's try again.
Today, let's talk about how to select contact information from "contacts". To put it bluntly, it is basically the same as the method for selecting files. We will see it later when we conduct instance drills.
Before getting started, we need to prepare some data, that is, the contact. If not, you can add a few to test.
Find and start the "contacts" application on the cute "start" screen.
If this is the first time you use it, you will be asked to enter the MS Account to obtain the contact, and we will follow the prompts. If you do not have a live ID, you can apply for a free chat.
After verifying and logging in successfully, you will see your friends.
If you do not have a contact, call out the toolbar and click "new.
As for the contact information, we are only testing at present. Therefore, just fill in with the example, as shown in.
After entering the information, click "save" to create a contact record. To facilitate testing, you can create n more records.
Now, with the data, we can start to practice.
1. Start beautiful vs2012 and create an app.
2. Open mainpage. XAML. The interface is as follows: The XAML layout is easy to read.
<Page X: class = "myappexample. mainpage "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: Local =" using: myappexample "xmlns: D =" http://schemas.microsoft.com/expression/blend/2008 "xmlns: MC =" http://schemas.openxmlformats.org/markup-compatibility/2006 "MC: ignorable = "D"> <grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "*"/> </grid. rowdefinitions> <stackpanel grid. row = "0" margin = "15, 27,"> <button content = "select contact" Click = "onpick"/> </stackpanel> <listview grid. row = "1" margin = "25" X: Name = "lvcontacts" scrollviewer. horizontalscrollmode = "Auto" scrollviewer. horizontalscrollbarvisibility = "Auto"> <listview. itemspanel> <itemspaneltemplate> <wrapgrid orientation = "vertical" maximumrowsorcolumns = "5" itemwidth = "300" layout = "center"/> </itemspaneltemplate> </listview. itemspanel> </listview> </GRID> </Page>
3. In the XAML document, find the click event handler of the button and select "locate to event handler" from the pop-up menu ".
The background processing code is as follows.
Using system; using system. collections. generic; using system. io; using system. LINQ; using Windows. foundation; using Windows. foundation. collections; using Windows. UI. XAML; using Windows. UI. XAML. controls; using Windows. UI. XAML. controls. primitives; using Windows. UI. XAML. data; using Windows. UI. XAML. input; using Windows. UI. XAML. media; using Windows. UI. XAML. navigation; using Windows. applicationModel. contacts; using Windows. UI. XAML. parameters; namespace myappexample {public sealed partial class mainpage: Page {public mainpage () {This. initializecomponent ();} private async void onpick (Object sender, routedeventargs e) {contactpicker cpicker = new contactpicker (); ireadonlylist <contactinformation> List = await cpicker. pickmultiplecontactsasync (); If (list = NULL) {return;} This. lvcontacts. items. clear (); foreach (VAR item in list) {textblock TB = new textblock (); TB. textwrapping = textwrapping. wrap; // The contact name is displayed as the title run runtitle = new run (); runtitle. fontsize = 22d; runtitle. fontweight = windows. UI. text. fontweights. bold; runtitle. TEXT = item. name; // Add the title text to textblock TB. inlines. add (runtitle); // A linefeed TB. inlines. add (New linebreak (); // read the contact's mobile phone number list foreach (VAR pH in item. phonenumbers) {run runno = new run (); runno. TEXT = string. format ("{0 }:{ 1}", pH. name, pH. value); TB. inlines. add (runno); TB. inlines. add (New linebreak ();} listviewitem vitem = new listviewitem (); // textblock is used as the content of listviewitem vitem. content = Tb; this. lvcontacts. items. add (vitem );}}}}
A. Remember to introduce the namespace windows. ApplicationModel. Contacts. The contactpicker class we use is under this namespace;
B. by calling the pickmultiplecontactsasync method, you can select multiple contacts at a time. It is an Asynchronous Method. Remember to add the waiting keyword;
C. The information of each contact is represented by a contactinformation instance. Because the selected contact information can be retrieved through the foreach loop, the name attribute indicates the name of the non-contact, in this example, we need to obtain the mobile phone number of the contact. Therefore, we can access the phonenumbers attribute because one contact may have n mobile phone numbers (for example, for work purposes, for family purposes, for example, whether it is the phonenumbers attribute or the emails attribute, It is a contactfield list, and each contactfield represents a field information, important attributes are generally read by name and value, for example:
Phoneno: 13521103823
Email: abcd@126.com
The name is phoneno, and the value is 13521103823.
Now we can run the application.