老周的博客http://blog.csdn.net/tcjiaan,转载请注明原作者和出处。

上回我们讨论了如何从联系人选择器中选择联系人记录。但,我们也许会发现一个问题,我们都知道,我们选择的联系人都是通过Microsoft帐号从云服务器取出来的,那么,如果我有自己的联系人数据呢?比如,保丰在本地数据文件中的,或者从我的Web服务中获取的联系人呢?那这种情况下,还能用联系人选择器来选择吗?

答案当然是肯定的,在Windows Store公开的API中,是允许我们对某些特殊的应用程序或UI进行自定义扩展,如前面我们提到的打开文件UI,保存文件UI,以及选取联系人信息UI都是可以扩展的。

这些扩展使用起来不难,只是刚刚接触的话,可能你会感到有些复杂,所以,还是那句话:熟能生巧,实践才能找到真理。

我们看看这个选择器是如何被扩展的,如果你安装过大智慧软件或者我们今天的应用,在选在联系人时,会看到下面的界面。

也就是说,这些扩展的应用,都集成到联系人选择器中了。

不多说了,Action!我们一边动手一边讨论吧。

1、启动VS for Win8,随便哪个版本,支持就行。新建项目,选择你喜欢的语言(C#),在模板中选择空白页面应用程序。

2、这样吧,我们把核心知识放到前面吧,先做好护展选择器的部分吧。

打开“解决方案资源管理器”窗口,在项目上右击,在弹出的菜单中选择“添加”-“新建项”,接着弹出一个新建项窗口,选择空白页,我们就命名为MyContactsPage.xaml。

这个页面不算复杂,只放一个ListView控件,这个页面就会成为我们启动联系人选择器时显示的列表。

<Page
x:Class="MyApp.MyContactsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<CollectionViewSource x:Key="cvs" x:Name="cvs"/>
</Page.Resources>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<ListView Name="lvContacts" ItemsSource="{Binding Source={StaticResource cvs}}" SelectionChanged="lvContacts_SelectionChanged_1" SelectionMode="Multiple"
ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollMode="Auto">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Vertical" MaximumRowsOrColumns="3" ItemWidth="380" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="8">
<TextBlock FontWeight="Bold" FontSize="20" Text="{Binding Name}" />
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="18" Text="手机号码:"/>
<TextBlock FontSize="18" Text="{Binding CellPhoneNo}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="18" Text="Email:"/>
<TextBlock FontSize="18" Text="{Binding EmailAddress}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>

注意页面的资源部分,我声明了一个CollectionViewSource,而ListView的数据源正是从它里面取,所以在ListView声明的时候,我们用Binding来获取数据。

<ListView Name="lvContacts" ItemsSource="{Binding Source={StaticResource cvs}}" ..........

也就是说,我们在后台代码中只需要为CollectionViewSource赋值即可。

3、在XAML视图中右击,从弹出的菜单中选择“查看代码”,切换到代码视图。

首先,我们需要定义一个自定义联系人类。这是我们用来测试的样本数据。

    public class CustContact
{
/// <summary>
/// 联系人名字
/// </summary>
public string Name { get; set; }
/// <summary>
/// 联系人手机号码
/// </summary>
public string CellPhoneNo { get; set; }
/// <summary>
/// 联系人电邮地址
/// </summary>
public string EmailAddress { get; set; }
/// <summary>
/// 获取测试数据源
/// </summary>
/// <returns></returns>
public static ObservableCollection<CustContact> GetContactSamples()
{
ObservableCollection<CustContact> mcList = new ObservableCollection<CustContact>();
mcList.Add(new CustContact { Name = "陈大冤", CellPhoneNo = "1388888552", EmailAddress = "ssdi@qq.com" });
mcList.Add(new CustContact { Name = "刘产", CellPhoneNo = "13712546810", EmailAddress = "zhe254@126.com" });
mcList.Add(new CustContact { Name = "鬼栋梁", CellPhoneNo = "18925445525", EmailAddress = "fuckdog@foxmail.com" });
mcList.Add(new CustContact { Name = "小气鬼", CellPhoneNo = "13628845781", EmailAddress= "ass054@21cn.com" });
mcList.Add(new CustContact { Name = "吴狗", CellPhoneNo = "13425546387", EmailAddress = "laobin@foxmail.com" });
mcList.Add(new CustContact { Name = "小赵", CellPhoneNo = "159999558462", EmailAddress = "hxdok@163.com" });
mcList.Add(new CustContact { Name = "老吹", CellPhoneNo = "1382601021", EmailAddress = "gaosu22@foxmail.com" });
mcList.Add(new CustContact { Name = "史珍香", CellPhoneNo = "10101010110", EmailAddress = "25466887@qq.com" });
mcList.Add(new CustContact { Name = "杜子腾", CellPhoneNo = "10084", EmailAddress = "bageyalu@21cn.com" });
mcList.Add(new CustContact { Name = "B哥", CellPhoneNo = "15525863314", EmailAddress = "ruxinde@126.com" });
return mcList;
}
}

4、处理ListView的SelectionChanged事件,以响应用户的选择操作。

        private void lvContacts_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
if (pkUI == null)
{
return;
}
ContactFieldFactory ctFactory = new ContactFieldFactory();
// 将选中的项添加到已选联系人列表
foreach (var itemAdded in e.AddedItems)
{
CustContact c = (CustContact)itemAdded;
if (!pkUI.ContainsContact(c.Name))
{
Contact contact = new Contact();
contact.Name = c.Name;
// 创建手机号码和电邮地址字段
ContactField phoneNoField = ctFactory.CreateField(c.CellPhoneNo, ContactFieldType.PhoneNumber);
ContactField emailField = ctFactory.CreateField(c.EmailAddress, ContactFieldType.Email);
// 别忘了把字段添加到联系人中
contact.Fields.Add(phoneNoField);
contact.Fields.Add(emailField);
pkUI.AddContact(c.Name, contact);
}
}
// 将未选中的联系人从已选列表中删除
foreach (var removedItem in e.RemovedItems)
{
CustContact ct = (CustContact)removedItem;
if (pkUI.ContainsContact(ct.Name))
{
pkUI.RemoveContact(ct.Name);
}
}
}

已经选择的项就添加到选择器的联系人列表中,而没有被选中的就从联系人列表中移除。添加联系人列表时(AddContact方法调用)有一个id参数,我们可以用联系人的名字作为其id值。

5、为了不影响生成的App代码,同时也方便我们维护,还是另起一个App类,并重写OnActivated方法。

    sealed partial class App : Application
{
protected override void OnActivated(Windows.ApplicationModel.Activation.IActivatedEventArgs args)
{
// 判断是否联系人选择器激活本应用程序
if (args.Kind == Windows.ApplicationModel.Activation.ActivationKind.ContactPicker)
{
Frame root = Window.Current.Content as Frame;
// 判断当前窗口是否有内容
if (root == null)
{
root = new Frame();
// 导航到选择联系人的页面
root.Navigate(typeof(MyContactsPage), (ContactPickerActivatedEventArgs)args);
// 将此Frame设置为当前窗口的内容
Window.Current.Content = root;
}
Window.Current.Activate(); //激活当前窗口
}
}
}

因为我们在页面的代码中要操作ContactPickerUI对象,所以将参数args传递到页面导航。

完整的代码如下。

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 System.Collections.ObjectModel;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Contacts;
using Windows.ApplicationModel.Contacts.Provider;
// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍
namespace MyApp
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class MyContactsPage : Page
{
ContactPickerUI pkUI = null;
public MyContactsPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.cvs.Source = CustContact.GetContactSamples();
if (e.Parameter != null)
{
if (e.Parameter is ContactPickerActivatedEventArgs)
{
this.pkUI = ((ContactPickerActivatedEventArgs)e.Parameter).ContactPickerUI;
}
}
}
private void lvContacts_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
if (pkUI == null)
{
return;
}
ContactFieldFactory ctFactory = new ContactFieldFactory();
// 将选中的项添加到已选联系人列表
foreach (var itemAdded in e.AddedItems)
{
CustContact c = (CustContact)itemAdded;
if (!pkUI.ContainsContact(c.Name))
{
Contact contact = new Contact();
contact.Name = c.Name;
// 创建手机号码和电邮地址字段
ContactField phoneNoField = ctFactory.CreateField(c.CellPhoneNo, ContactFieldType.PhoneNumber);
ContactField emailField = ctFactory.CreateField(c.EmailAddress, ContactFieldType.Email);
// 别忘了把字段添加到联系人中
contact.Fields.Add(phoneNoField);
contact.Fields.Add(emailField);
pkUI.AddContact(c.Name, contact);
}
}
// 将未选中的联系人从已选列表中删除
foreach (var removedItem in e.RemovedItems)
{
CustContact ct = (CustContact)removedItem;
if (pkUI.ContainsContact(ct.Name))
{
pkUI.RemoveContact(ct.Name);
}
}
}
}
public class CustContact
{
/// <summary>
/// 联系人名字
/// </summary>
public string Name { get; set; }
/// <summary>
/// 联系人手机号码
/// </summary>
public string CellPhoneNo { get; set; }
/// <summary>
/// 联系人电邮地址
/// </summary>
public string EmailAddress { get; set; }
/// <summary>
/// 获取测试数据源
/// </summary>
/// <returns></returns>
public static ObservableCollection<CustContact> GetContactSamples()
{
ObservableCollection<CustContact> mcList = new ObservableCollection<CustContact>();
mcList.Add(new CustContact { Name = "陈大冤", CellPhoneNo = "1388888552", EmailAddress = "ssdi@qq.com" });
mcList.Add(new CustContact { Name = "刘产", CellPhoneNo = "13712546810", EmailAddress = "zhe254@126.com" });
mcList.Add(new CustContact { Name = "鬼栋梁", CellPhoneNo = "18925445525", EmailAddress = "fuckdog@foxmail.com" });
mcList.Add(new CustContact { Name = "小气鬼", CellPhoneNo = "13628845781", EmailAddress= "ass054@21cn.com" });
mcList.Add(new CustContact { Name = "吴狗", CellPhoneNo = "13425546387", EmailAddress = "laobin@foxmail.com" });
mcList.Add(new CustContact { Name = "小赵", CellPhoneNo = "159999558462", EmailAddress = "hxdok@163.com" });
mcList.Add(new CustContact { Name = "老吹", CellPhoneNo = "1382601021", EmailAddress = "gaosu22@foxmail.com" });
mcList.Add(new CustContact { Name = "史珍香", CellPhoneNo = "10101010110", EmailAddress = "25466887@qq.com" });
mcList.Add(new CustContact { Name = "杜子腾", CellPhoneNo = "10084", EmailAddress = "bageyalu@21cn.com" });
mcList.Add(new CustContact { Name = "B哥", CellPhoneNo = "15525863314", EmailAddress = "ruxinde@126.com" });
return mcList;
}
}
sealed partial class App : Application
{
protected override void OnActivated(Windows.ApplicationModel.Activation.IActivatedEventArgs args)
{
// 判断是否联系人选择器激活本应用程序
if (args.Kind == Windows.ApplicationModel.Activation.ActivationKind.ContactPicker)
{
Frame root = Window.Current.Content as Frame;
// 判断当前窗口是否有内容
if (root == null)
{
root = new Frame();
// 导航到选择联系人的页面
root.Navigate(typeof(MyContactsPage), (ContactPickerActivatedEventArgs)args);
// 将此Frame设置为当前窗口的内容
Window.Current.Content = root;
}
Window.Current.Activate(); //激活当前窗口
}
}
}
}

6、回过头来,我们在看看主页。

打开MainPage.xaml,参考以下XAML。

<Page
x:Class="MyApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
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" Orientation="Horizontal">
<Button Margin="15,12,7,6" Content="选择联系人" Click="onSelect"/>
<TextBlock Name="tbMsg" Margin="8,23,0,6" FontSize="18"/>
</StackPanel>
<ListView Name="lv" Grid.Row="1" Margin="7">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="3" ItemWidth="350"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
</Page>

7、后台代码如下。

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;
// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍
namespace MyApp
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void onSelect(object sender, RoutedEventArgs e)
{
ContactPicker cp = new ContactPicker();
IReadOnlyList<ContactInformation> contactlist = await cp.PickMultipleContactsAsync();
if (contactlist != null )
{
foreach (var item in contactlist)
{
string msg = "";
msg += item.Name + "\n";
//手机号码
foreach (var fds in item.PhoneNumbers)
{
msg += string.Format("{0} : {1}", fds.Name, fds.Value) + "\n";
}
//电邮地址
foreach (var emfds in item.Emails)
{
msg += string.Format("{0} : {1}", emfds.Name, emfds.Value) + "\n";
}
this.lv.Items.Add(msg);
this.tbMsg.Text = string.Format("你已选择了{0}条联系人记录。", contactlist.Count);
}
}
}
}
}

这个不用解释了,和上一篇文章中是一样的。

8、打开清单文件,切换到“声明”选项卡,从下拉列表中选择“联系人选取器”,点击右边的“添加”按钮。

现在,可以运行应用了。点击选择联系人。

选择后,点确定。回到主页面,就看到我们刚才选择的联系人了。

本文内容可能不太好理解,所以,稍后我会把源代码上传到资源。

最后,祝大家中秋节快乐。

新时尚Windows8开发(15):扩展联系人选择器相关推荐

  1. 新时尚Windows8开发(21):分组视图

    转自:http://blog.csdn.net/tcjiaan/article/details/8069349 有时候,数据的量比较大,有可以我们需要对其进行分组,以方便查看,就像系统的应用程序列表一 ...

  2. 新时尚Windows8开发(25):缩放视图

    转自:http://blog.csdn.net/tcjiaan/article/details/8120584 前面有一节,我们探讨了分组视图,本节我们再来吹一下有关缩放视图.那么,这视图怎么个缩放法 ...

  3. win8开发(15)——扩展联系人选择器

    上回我们讨论了如何从联系人选择器中选择联系人记录.但,我们也许会发现一个问题,我们都知道,我们 选择的联系人都是通过Microsoft帐号从云服务器取出来的,那么,如果我有自己的联系人数据呢?比如, ...

  4. 牛仔新时尚-小程序开发案例-数据库篇

    今天我将以「牛仔新时尚」小程序为例,服装行业订单小程序从设计到实现的全过程.主要讲产品逻辑搭建和数据库设计的过程.这个小程序的主要功能是向用户展示服装和面料商品,并提供搜索.收藏商品,加入购物车和下单 ...

  5. 【Windows8开发】关于WinRT组件,WinRT dll,Win32 dll,WinRT exe组件的一些尝试

    随着Win8的推出,提出了很多新的概念,比如WinRT Component,WinRT dll,WinRT exe component等.基于这些新的概念,进行了很多尝试,本文会把结果分享给大家,希望 ...

  6. 使用Rekit开发可扩展的前端应用

    近几年前端技术的快速发展,开发能力与开发难度在逐渐上升.一方面,大型项目中的技术选型,项目结构组织问题一直没有统一的实践方式.另一方面,前端项目的开发工具远远落后于技术本身的发展.大家现在使用的VSC ...

  7. 【逆向知识】开发WinDBG扩展DLL

    如何开发WinDbg扩展DLL WinDbg扩展DLL是一组导出的回调函数,用于实现用户定义的命令.以便从内存转储中提取特定的信息.扩展dll由调试器引擎加载,可以在执行用户模式或内核模式调试时提供自 ...

  8. 你好,SegmentFault 新导航 【开发手册】; 再见,侧边导航栏

    SegmentFault 在夏季上线了 SegmentFault 5.0 版本,迭代了整个主站的样式,在 8 月的时候上线了面板九宫格(侧边导航栏),整合了部分的入口,方便用户查看笔记.徽章和排行榜等 ...

  9. android仿微信点击好友,安卓开发仿微信联系人列表-机器人列表视图仿微通道聊天多久最底部滑动...

    楼主你好!根据你的描述,让我给你答案! :新内容加进来,列表视图重新为setSelection后,定位结束后,拉起一个页面放. . 希望你能有所帮助,如果满意,请记得采纳像下拉条为微信好友如何实现 简 ...

最新文章

  1. 【求锤得锤的故事】Redis锁从面试连环炮聊到神仙打架。
  2. jQuery-easyui和validate表单验证实例
  3. 黑马程序员C语言基础(第七天)内存管理
  4. http和https和ssl和tcp/ip之间的关系和区别
  5. property修饰关键字
  6. Windows误关闭资源管理器重启的办法
  7. html中的数字选框,带有复选框和数字类型的HTML表单提交与PHP?
  8. SpringSecurity入门到入土教程_1
  9. 一体机or复合机?企业文印设备该怎么选
  10. anaconda 导入cv2
  11. Nginx入门5:搭建静态资源服务器;(入门级演示,没多少内容;)
  12. FISCO BCOS区块链搭建说明(第一篇)
  13. flashfxp配置文件服务器同步,如何导出FlashFXP的站点配置文件
  14. docker之user_remap
  15. Jacoco代码覆盖率报告详解
  16. 从平台到中台 | Elasticsearch 在蚂蚁金服的实践经验
  17. 全世界正在被软件占领
  18. 怎么使用迅捷文字转语音软件转换文字
  19. 专题:设计模式(精华篇)(Yanlz+单一职责+里氏替换+依赖倒置+接口隔离+迪米特+开放封闭+创建类+结构类+行为类+立钻哥哥)
  20. Github pages个人域名添加SSL

热门文章

  1. 世纪安图三区三线质检解决方案
  2. windows中相对路径和绝对路径
  3. linux系统下的打印机驱动下载,总结各大常见打印机品牌在Linux下的驱动方法
  4. 厦门航空维修岗位————福州大学站
  5. python股票买卖问题_Python分析股票买卖点 2
  6. 详解双极结型晶体管的工作原理
  7. 浅谈“大数据”与“航空配餐大数据”
  8. 什么是FPGA?为什么FPGA会如此重要?
  9. 大数据-11-案例演习-淘宝双11数据分析与预测
  10. “找你妹”的积分墙盈利之道