原文:重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口

[源码下载]

重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 选取器

  • ContactPicker - 联系人选取器
  • ContactPickerUI - 自定义联系人选取器

示例
演示如何通过 ContactPicker 选择一个或多个联系人,以及如何开发自定义联系人选取器

1、 开发一个自定义联系人选取器
Picker/MyContactPicker.xaml

<Page
x: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(, ).ToString();
contact.Fields.Add(new ContactField(random.Next(, ).ToString(), ContactFieldType.Email, ContactFieldCategory.Home));
contact.Fields.Add(new ContactField(random.Next(, ).ToString(), ContactFieldType.Email, ContactFieldCategory.Work));
contact.Fields.Add(new ContactField(random.Next(, ).ToString(), ContactFieldType.PhoneNumber, ContactFieldCategory.Home));
contact.Fields.Add(new ContactField(random.Next(, ).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

<Page
x: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 > )
{
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 > )
{
ContactInformation contact = contacts[]; 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 > )
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream);
imgThumbnail.Source = bitmap;
}
}
else
{
lblMsg.Text = "取消了";
}
}
}
}
}

OK
[源码下载]

重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口的更多相关文章

  1. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  2. 重新想象 Windows 8 Store Apps (28) - 选取器: CachedFileUpdater(缓存文件更新程序)

    原文:重新想象 Windows 8 Store Apps (28) - 选取器: CachedFileUpdater(缓存文件更新程序) [源码下载] 重新想象 Windows 8 Store App ...

  3. 重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口

    原文:重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (26) ...

  4. 重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口

    原文:重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (2 ...

  5. 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo

    [源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...

  6. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解

    [源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...

  7. 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解

    [源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...

  8. 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract

    [源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  9. 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract

    [源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...

随机推荐

  1. delphi 回调函数

    program Project2; {$APPTYPE CONSOLE} uses SysUtils; type //定义一个对象事件方法 TCallbackFunc = function (i: I ...

  2. latex命令替换之\newcommand

    有时候我们在用latex写文档的时候不想写很长的命令,那么我们自己定义一个新的命令来替换一段代码. 举例如下: \usepackage{booktabs} \usepackage{multirow} ...

  3. JavaEE session机制

    JavaEE session机制 Http协议: 在讲session之前,必须说下Http协议,HTTP是一个client和server端请求和应答的标准(TCP).由HTTPclient发起一个请求 ...

  4. 从零開始制作H5应用(4)——V4.0,增加文字并给文字加特效

    之前,我们分三次完毕了我们第一个H5应用的三个迭代版本号: V1.0--简单页面滑动切换 V2.0--多页切换,透明过渡及交互指示 V3.0--加入loading,music及自己主动切换 这已经是一 ...

  5. winXP JDK由1.8改为1.6

    (1)直接在环境变量中删除配置的相关路径 path的值: C:\Documents and Settings\Administrator>path PATH=C:\Documents and S ...

  6. hdu1428之spfa+dfs

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. 使用CXF创建REST WEBSERVICE

    简单小结下CXF跟REST搭配webservice的做法,直接举代码为样例: 1 order.java   package com.example.rest; import javax.xml.bin ...

  8. 完整导出IntelliJ IDEA的快捷键

    工欲善其事,必先利其器. 常常和代码打交道的人,熟练使用IDE快捷键那是必须的,由于快捷键能够把你从各种罗嗦事中解放出来.比方,假设没有快捷键,你就须要常常性的暂停快速执行的大脑,右手凭记忆摸到鼠标, ...

  9. 使用javaDate类代数据仓库维度表

    使用javaDate类代数据仓库维度表 Date类别: ,返回一个相对日期的毫秒数.精确到毫秒.但不支持日期的国际化和分时区显示. Date 类从Java 开发包(JDK)1.0 就開始进化,当时它仅 ...

  10. TMG 2010 VPN配置

    微软的ISA 到2006以后就叫TMG了,上周在公司的服务器上安装测试了下,虽然增加了很多功能,但是主要功能上和ISA 2004差不多,最近在部署L2TP VPN,由于防火墙带的远程访问VPN为纯的L ...