重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口...
[源码下载]
作者:webabcd
介绍
重新想象 Windows 8 Store Apps 之 选取器
- ContactPicker - 联系人选取器
- ContactPickerUI - 自定义联系人选取器
示例
演示如何通过 ContactPicker 选择一个或多个联系人,以及如何开发自定义联系人选取器
1、 开发一个自定义联系人选取器
Picker/MyContactPicker.xaml
<Pagex:Class="XamlDemo.Picker.MyContactPicker"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:XamlDemo.Picker"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"><Grid Background="Transparent"><StackPanel Margin="120 0 0 0"><TextBlock Name="lblMsg" FontSize="14.667" /><Button Name="btnAddContract" Content="增加一个联系人" Click="btnAddContract_Click" Margin="0 10 0 0" /></StackPanel></Grid> </Page>
Picker/MyContactPicker.xaml.cs
/** 演示如何开发自定义的联系人选取器* * 1、在 Package.appxmanifest 中新增一个“联系人选取器”声明,并做相关配置* 2、在 App.xaml.cs 中 override void OnActivated(IActivatedEventArgs args),以获取联系人选取器的相关信息* * ContactPickerActivatedEventArgs - 通过“联系人选取器”激活应用程序时的事件参数* ContactPickerUI - 获取 ContactPickerUI 对象* PreviousExecutionState, Kind, SplashScreen - 各种激活 app 的方式的事件参数基本上都有这些属性,就不多说了* * ContactPickerUI - 自定义联系人选取器的帮助类* SelectionMode - 获取由 ContactPicker(调用者)设置的 SelectionMode 属性* DesiredFields - 获取由 ContactPicker(调用者)设置的 DesiredFields 属性* AddContact(string id, Contact contact) - 选取一个联系人* id - 联系人标识* contact - 一个 Contact 对象* RemoveContact() - 删除指定标识的联系人* ContainsContact() - 指定标识的联系人是否已被选取* ContactRemoved - 移除一个已被选取的联系人时所触发的事件* * Contact - 返回给调用者的联系人对象* Name - 名称* Thumbnail - 缩略图* Fields - 联系人的字段数据,每一条数据都是一个实现了 IContactField 接口的对象* * ContactField - 实现了 IContactField 接口,用于描述联系人的某一个字段数据* Type - 字段类型(ContactFieldType 枚举)* Email, PhoneNumber, Location, InstantMessage, Custom* Category - 字段类别(ContactFieldCategory 枚举)* None, Home, Work, Mobile, Other* Value - 字段的值*/using System; using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.Contacts.Provider; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; using Windows.ApplicationModel.Contacts; using Windows.Storage.Streams; using Windows.UI.Core;namespace XamlDemo.Picker {public sealed partial class MyContactPicker : Page{private ContactPickerUI _contactPickerUI;public MyContactPicker(){this.InitializeComponent();}protected override void OnNavigatedTo(NavigationEventArgs e){// 获取 ContactPickerUI 对象var contactPickerActivated = e.Parameter as ContactPickerActivatedEventArgs;_contactPickerUI = contactPickerActivated.ContactPickerUI;_contactPickerUI.ContactRemoved += _contactPickerUI_ContactRemoved; }protected override void OnNavigatedFrom(NavigationEventArgs e){_contactPickerUI.ContactRemoved -= _contactPickerUI_ContactRemoved;}// 从选取缓冲区移除后async void _contactPickerUI_ContactRemoved(ContactPickerUI sender, ContactRemovedEventArgs args){// 注意:无法直接得知 ContactPickerUI 是单选模式还是多选模式,需要判断当添加了一个联系人后,再添加一个联系人,如果系统会自动移除前一个联系人,则说明是单选模式await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>{lblMsg.Text += "removed contact: " + args.Id;lblMsg.Text += Environment.NewLine;});}private void btnAddContract_Click(object sender, RoutedEventArgs e){Random random = new Random();// 构造一个 Contact 对象Contact contact = new Contact();contact.Name = "webabcd " + random.Next(1000, 10000).ToString();contact.Fields.Add(new ContactField(random.Next(1000, 10000).ToString(), ContactFieldType.Email, ContactFieldCategory.Home));contact.Fields.Add(new ContactField(random.Next(1000, 10000).ToString(), ContactFieldType.Email, ContactFieldCategory.Work));contact.Fields.Add(new ContactField(random.Next(1000, 10000).ToString(), ContactFieldType.PhoneNumber, ContactFieldCategory.Home));contact.Fields.Add(new ContactField(random.Next(1000, 10000).ToString(), ContactFieldType.PhoneNumber, ContactFieldCategory.Work));contact.Thumbnail = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Logo.png", UriKind.Absolute));string id = Guid.NewGuid().ToString();// 向选取缓冲区新增一个联系人switch (_contactPickerUI.AddContact(id, contact)){case AddContactResult.Added: // 已被成功添加lblMsg.Text += "added contact: " + id;lblMsg.Text += Environment.NewLine;break;case AddContactResult.AlreadyAdded: // 选取缓冲区已有此联系人lblMsg.Text += "already added contact: " + id;lblMsg.Text += Environment.NewLine;break;case AddContactResult.Unavailable: // 无效联系人lblMsg.Text += "unavailable contact: " + id;lblMsg.Text += Environment.NewLine;break;}}} }
2、判断程序是否是由联系人选取器激活,在 App.xaml.cs 中 override void OnActivated(IActivatedEventArgs args)
App.xaml.cs
protected override void OnActivated(IActivatedEventArgs args) {// 通过联系人选取器激活应用程序时if (args.Kind == ActivationKind.ContactPicker){ContactPickerActivatedEventArgs contactPickerArgs = args as ContactPickerActivatedEventArgs;Frame rootFrame = new Frame();rootFrame.Navigate(typeof(MainPage), contactPickerArgs);Window.Current.Content = rootFrame;Window.Current.Activate();} }
3、通过联系人选取器选择联系人。注:如果需要激活自定义的联系人选取器,请在弹出的选取器窗口的左上角选择对应 Provider
Picker/ContactPickerDemo.xaml
<Pagex:Class="XamlDemo.Picker.ContactPickerDemo"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:XamlDemo.Picker"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"><Grid Background="Transparent"><StackPanel Margin="120 0 0 0"><TextBlock Name="lblMsg" FontSize="14.667" /><Image Name="imgThumbnail" Width="100" Height="100" HorizontalAlignment="Left" Margin="0 10 0 0" /><Button Name="btnPickContact" Content="pick a contact" Click="btnPickContact_Click" Margin="0 10 0 0" /><Button Name="btnPickContacts" Content="pick multiple contacts" Click="btnPickContacts_Click" Margin="0 10 0 0" /></StackPanel></Grid> </Page>
Picker/ContactPickerDemo.xaml.cs
/** 演示如何通过 ContactPicker 选择一个或多个联系人* * ContactPicker - 联系人选择窗口* CommitButtonText - 联系人选择窗口的确定按钮的显示文本,此按钮默认显示的文本为“确定”* SelectionMode - 选取模式(ContactSelectionMode 枚举)* Contacts - 请对我提供联系人的全部字段的数据,默认值* Fields - 请对我提供指定字段的数据* DesiredFields - 当 SelectionMode.Fields 时,请对我提供指定字段的数据,字段名称来自 KnownContactField 枚举* PickSingleContactAsync() - 选取一个联系人,返回 ContactInformation 对象* PickMultipleContactsAsync() - 选取多个联系人,返回 ContactInformation 对象集合* * ContactInformation - 联系人信息对象* Name, Emails, PhoneNumbers, Locations, InstantMessages, CustomFields* GetThumbnailAsync() - 获取联系人缩略图*/using System; using System.Collections.Generic; using System.Linq; using Windows.ApplicationModel.Contacts; using Windows.Storage.Streams; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Imaging; using XamlDemo.Common;namespace XamlDemo.Picker {public sealed partial class ContactPickerDemo : Page{public ContactPickerDemo(){this.InitializeComponent();}private async void btnPickContact_Click(object sender, RoutedEventArgs e){if (Helper.EnsureUnsnapped()){ContactPicker contactPicker = new ContactPicker();contactPicker.CommitButtonText = "确定";contactPicker.SelectionMode = ContactSelectionMode.Contacts;// 启动联系人选取器,以选择一个联系人ContactInformation contact = await contactPicker.PickSingleContactAsync();if (contact != null){lblMsg.Text = "name: " + contact.Name;lblMsg.Text += Environment.NewLine;lblMsg.Text += "emails: " + string.Join(",", contact.Emails.Select(p => new { email = p.Value }));lblMsg.Text += Environment.NewLine;lblMsg.Text += "phoneNumbers: " + string.Join(",", contact.PhoneNumbers.Select(p => new { phoneNumber = p.Value }));IRandomAccessStreamWithContentType stream = await contact.GetThumbnailAsync();if (stream != null && stream.Size > 0){BitmapImage bitmap = new BitmapImage();bitmap.SetSource(stream);imgThumbnail.Source = bitmap;}}else{lblMsg.Text = "取消了";}}}private async void btnPickContacts_Click(object sender, RoutedEventArgs e){if (Helper.EnsureUnsnapped()){var contactPicker = new ContactPicker();// 启动联系人选取器,以选择多个联系人IReadOnlyList<ContactInformation> contacts = await contactPicker.PickMultipleContactsAsync();if (contacts != null && contacts.Count > 0){ContactInformation contact = contacts[0];lblMsg.Text = "contacts count: " + contacts.Count.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "first contact name: " + contact.Name;lblMsg.Text += Environment.NewLine;lblMsg.Text += "first contact emails: " + string.Join(",", contact.Emails.Select(p => new { email = p.Value }));lblMsg.Text += Environment.NewLine;lblMsg.Text += "first contact phoneNumbers: " + string.Join(",", contact.PhoneNumbers.Select(p => new { phoneNumber = p.Value }));IRandomAccessStreamWithContentType stream = await contact.GetThumbnailAsync();if (stream != null && stream.Size > 0){BitmapImage bitmap = new BitmapImage();bitmap.SetSource(stream);imgThumbnail.Source = bitmap;}}else{lblMsg.Text = "取消了";}}}} }
OK
[源码下载]
重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口...相关推荐
- 重新想象 Windows 8 Store Apps (61) - 通信: http, oauth
重新想象 Windows 8 Store Apps (61) - 通信: http, oauth 原文:重新想象 Windows 8 Store Apps (61) - 通信: http, oauth ...
- 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom...
原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...
- 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop...
重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 原文:重新想象 Window ...
- 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换...
重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换 原文: ...
- 重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础
原文:重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础 [源码下载] 重新想象 Windows 8 Store Apps (9) - 控件之 Sc ...
- 重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider...
重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider 原文:重新想象 Windows 8 S ...
- 重新想象 Windows 8 Store Apps (59) - 锁屏
原文:重新想象 Windows 8 Store Apps (59) - 锁屏 [源码下载] 重新想象 Windows 8 Store Apps (59) - 锁屏 作者:webabcd 介绍 重新想象 ...
- Unity Game Starter Kit for Windows Store and Windows Phone Store games
原地址:http://digitalerr0r.wordpress.com/2013/09/30/unity-game-starter-kit-for-windows-store-and-window ...
- 【万里征程——Windows App开发】文件数据——文件选取器
使用文件选取器保存文件 就我个人而言,还是非常喜欢使用文件选取器的,因为能够用自己的代码来调用系统的各种弹框. 在这个示例中,首先在XAML中添加一个Button和一个TextBlock,分别命名为b ...
最新文章
- mysql处理存在则更新,不存在则插入(多列唯一索引)
- FTP服务器配置与管理(4) 服务器端的常用配置及FTP命令
- 手把手教你学Dapr - 1. .Net开发者的大时代
- python学习过程中随手写的测试脚本-testloop.py
- 用python设计学生管理系统_Python实现GUI学生信息管理系统
- mysql 5.7.20 win64_Win10下MySQL5.7.20 Mysql(64位)解压版安装及bug修复
- 系统架构与软件架构是一层含义吗
- 【c语言】小游戏程序——弹跳小球
- 正则表达式(二)常用正则表达式——验证真实姓名
- python5个标准库,列出5个python标准库
- 直流稳压电源基本概念及使用方法入门
- maya导入abc动画_外包过程中的动画重定向以及蒙皮调整经验
- 如何用python制作动画电影_动画电影是如何制作的?
- 梦幻诛仙linux纯端架设教程,梦幻诛仙 一键端搭建iOS安卓双端+完整后台源码+各种工具附带视频架设教程...
- 关于网络渗透的过程以及感想记录
- android 吧文字读出来,android学习之文字语音朗读
- 106短信平台多少钱一条比较合理?
- 电脑文件不小心删除了怎么恢复 ? 删除的文件如何恢复文件?
- 5W1H聊开源之Who——谁“发明”了开源?
- Introduction to modern cryptography 第一章阅读笔记
热门文章
- RE:大家说说开发的时候类名和文件名一般是怎么规范的?
- Java实现阶乘运算
- 敏捷项目向组合级看齐
- preparestatement方法用多次_如何用java 5分钟实现一个最简单的mysql代理服务器?
- vsphere通用配置_Mac环境下如何用Hexo+Github搭建个人博客
- 除了分析引擎 2.0,神策再发一波儿新功能!
- 在Spring中使用JTA事务管理
- “智能微尘”:助推物联网应用的关键
- ALGORITHM IMPORTANT QUESTIONS
- 行业研究报告基本分析思路updated with 5c model