原文:重新想象 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. Access Violation at address 00000000.Read of address 00000000 解决办法

    是数组越标或没有初始化某个对象之类的问题,搂住细细检查一下代码, 使用指针前未做检查,而这个指针未初始化. 可能是new后没有delete,这样出现溢出的可能性比较大     检查代码或者跟踪试试 使 ...

  2. 黑马程序员:Java基础总结----泛型(高级)

    黑马程序员:Java基础总结 泛型(高级)   ASP.Net+Android+IO开发 . .Net培训 .期待与您交流! 泛型(高级) 泛型是提供给javac编译器使用的,可以限定集合中的输入类型 ...

  3. 访何红辉:谈谈Android源码中的设计模式

    最近Android 6.0版本的源代码开放下载,刚好分析Android源码的技术书籍<Android源码设计模式解析与实战>上市,我们邀请到它的作者何红辉,来谈谈Android源码中的设计 ...

  4. [gkk]传智-适配器设计模式,如同电源适配器

    //适配器设计模式 是图形化设计中用的.如同电源适配器 import java.awt.*; inport java.awte public calss MyFrame{ public static ...

  5. 探索Oracle之数据库升级七 11gR2 to 12c 升级完毕后插入PDB

    探索Oracle之数据库升级七 11gR2 to 12c 升级完毕后插入PDB 前言:        从Oracle 12c開始,引入了容器数据库的概念,能够实现数据库插拔操作,例如以下图: 如今我们 ...

  6. Merging a WPF application into a single EXE(WPF应用程序合并成单个Exe文件)

    I always dislike handing off little applications to people. Not because I can’t, but because of the ...

  7. Deep learning From Image to Sequence

    本文笔记旨在概括地讲deep learning的经典应用.内容太大,分三块. ------------------------------------------------------------- ...

  8. iOS 同步GET

    (注意: 能够整片复制)

  9. 《算法导论》 — Chapter 7 高速排序

    序 高速排序(QuickSort)也是一种排序算法,对包括n个数组的输入数组.最坏情况执行时间为O(n^2). 尽管这个最坏情况执行时间比較差.可是高速排序一般是用于排序的最佳有用选择.这是由于其平均 ...

  10. 【CSS3】transform-origin原点旋转

    忙乱, 点 -moz-transform-origin: 0 0; -webkit-transform-origin:0 0; -o-transform-origin:0 0; 以右上角给原点 -mo ...