老周的博客http://blog.youkuaiyun.com/tcjiaan,转载请注明原作者和出处。
前面我们讲了如何用文件选择器选取文件,其实,还有一个目录选择器,不过,我先放着不说,因为它用起来和选取文件是一个道理,大家不妨尝试一下举一反六。
今天,我们说一说如何从“人脉”中选取联系人信息,说白了,也和文件选取的方法基本一样,后面做实例演练的时候,大家会看到的。
在开始实践之前,我们需要准备一些数据,即联系人,如果没有,你可以随便加几个来测试。
从可爱的“开始”屏幕中找到“人脉”应用,并启动。
如果是第一次使用,会要求你输入MS帐号获取联系人,我们就按提示操作,如果你没有Live ID,就随便申请一个呗。
验证并登陆成功后,你就会看到你的好友们了。
如果你还没有联系人,调出工具条,点击“新建”按钮。
至于联系人信息,我们目前只是测试,所以,随例填就行了,如下图所示。
填完后,点击“保存”,这样就创建了联系人记录。为了方便测试,你可以多建N条记录。
好了,有了数据,我们就可以动手练习了。
1、启动美丽的VS2012,新建一个app应用。
2、打开MainPage.xaml,界面这按下面的XAML布局即可,简便易读嘛。
<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,0,22">
<Button Content="选取联系人" 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" VerticalChildrenAlignment="Center"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
</Page>
3、在XAML文档中,找到Button的Click事件处理程序,从弹出的菜单中选择“定位到事件处理程序”。
后台的处理代码,如下。
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.Documents;
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;
// 联系人名字显示为标题
Run runTitle = new Run();
runTitle.FontSize = 22d;
runTitle.FontWeight = Windows.UI.Text.FontWeights.Bold;
runTitle.Text = item.Name;
// 将标题文本添加到TextBlock中
tb.Inlines.Add(runTitle);
// 一个换行符
tb.Inlines.Add(new LineBreak());
// 读出联系人的手机号码列表
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作为ListViewItem的内容
vitem.Content = tb;
this.lvContacts.Items.Add(vitem);
}
}
}
}
a、记得引入命名空间Windows.ApplicationModel.Contacts,我们用到的ContactPicker类就在该命名空间下;
b、通过调用PickMultipleContactsAsync方法,可以一次性选择多位联系人,它是一个异步方法,记得加上等待关键字;
c、每一位联系人的信息,由一个ContactInformation实例表示,因为通过foreach循环可以取出所选择的联系人信息,其中Name属性表示不联系人的名字,本例我们需要取得联系人的手机号码,因而我们访问PhoneNumbers属性,由于一位联系人可能拥用N个手机号码(比如工作用的,家庭用的,泡妞专用的),所以,无论是PhoneNumbers属性也好,Emails属性也罢,都是一个ContactField列表,而每个ContactField就代表一个字段信息,重要的属性我们一般会读取Name和Value,比如:
phoneno : 13521103823
email : abcd@126.com
Name就是phoneno,Value就是13521103823。
现在,我们可以运行应用程序了。