[源码下载]

背水一战 Windows 10 (96) - 选取器: ContactPicker

作者:webabcd

介绍
背水一战 Windows 10 之 选取器

  • ContactPicker(联系人选取窗口)
  • 通过 ContactPicker 选取联系人,并获取其完整信息

示例
1、演示如何通过 ContactPicker 选择一个或多个联系人
Picker/ContactPickerDemo.xaml

<Page
x:Class="Windows10.Picker.ContactPickerDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.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="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnPickContact" Content="pick a contact" Click="btnPickContact_Click" Margin="5" /> <Button Name="btnPickContacts" Content="pick multiple contacts" Click="btnPickContacts_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>

Picker/ContactPickerDemo.xaml.cs

/* 演示如何通过 ContactPicker 选择一个或多个联系人
*
* ContactPicker - 联系人选择窗口(有好多 api 在 uwp 中废弃了)
* DesiredFieldsWithContactFieldType - 需要获取的联系人的字段(可以添加 ContactFieldType 类型的枚举)
* PickContactAsync() - 弹出联系人选取器(只能选取一个),返回 Contact 类型的对象
* PickContactsAsync() - 弹出联系人选取器(可以选取多个),返回 Contact 类型的对象
*
* Contact - 联系人对象
* 有一堆属性,看文档吧
*/ using System;
using System.Collections.Generic;
using Windows.ApplicationModel.Contacts;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Picker
{
public sealed partial class ContactPickerDemo : Page
{
public ContactPickerDemo()
{
this.InitializeComponent();
} private async void btnPickContact_Click(object sender, RoutedEventArgs e)
{
ContactPicker contactPicker = new ContactPicker();
// 指定需要选取的联系人的字段
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email);
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber); // 启动联系人选取器,以选择一个联系人
Contact contact = await contactPicker.PickContactAsync(); if (contact != null)
{
lblMsg.Text += string.Format("name:{0}", contact.Name);
lblMsg.Text += Environment.NewLine; foreach (ContactEmail email in contact.Emails)
{
lblMsg.Text += string.Format("email kind:{0}, email address:{1}", email.Kind, email.Address);
lblMsg.Text += Environment.NewLine;
} foreach (ContactPhone phone in contact.Phones)
{
lblMsg.Text += string.Format("phone kind:{0}, phone number:{1}, phone description:{2}", phone.Kind, phone.Number, phone.Description);
lblMsg.Text += Environment.NewLine;
}
}
else
{
lblMsg.Text += "取消了";
lblMsg.Text += Environment.NewLine;
}
} private async void btnPickContacts_Click(object sender, RoutedEventArgs e)
{
ContactPicker contactPicker = new ContactPicker();
// 指定需要选取的联系人的字段
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email);
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber); // 启动联系人选取器,以选择多个联系人
IList<Contact> contacts = await contactPicker.PickContactsAsync(); if (contacts != null && contacts.Count > )
{
foreach (Contact contact in contacts)
{
lblMsg.Text += string.Format("name:{0}", contact.Name);
lblMsg.Text += Environment.NewLine; foreach (ContactEmail email in contact.Emails)
{
lblMsg.Text += string.Format("email kind:{0}, email address:{1}", email.Kind, email.Address);
lblMsg.Text += Environment.NewLine;
} foreach (ContactPhone phone in contact.Phones)
{
lblMsg.Text += string.Format("phone kind:{0}, phone number:{1}, phone description:{2}", phone.Kind, phone.Number, phone.Description);
lblMsg.Text += Environment.NewLine;
}
}
}
else
{
lblMsg.Text += "取消了";
lblMsg.Text += Environment.NewLine;
}
}
}
}

2、演示如何通过 ContactPicker 选取联系人,并获取其完整信息
Picker/ContactPicker2.xaml

<Page
x:Class="Windows10.Picker.ContactPicker2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.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="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Image Name="imgThumbnail" Width="100" Height="100" HorizontalAlignment="Left" Margin="5" /> <Button Name="btnPickContact" Content="pick a contact" Click="btnPickContact_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>

Picker/ContactPicker2.xaml.cs

/* 演示如何通过 ContactPicker 选取联系人,并获取其完整信息
*
* ContactPicker - 联系人选择窗口(有好多 api 在 uwp 中废弃了)
* DesiredFieldsWithContactFieldType - 需要获取的联系人的字段(可以添加 ContactFieldType 类型的枚举)
* PickContactAsync() - 弹出联系人选取器(只能选取一个),返回 Contact 类型的对象
* PickContactsAsync() - 弹出联系人选取器(可以选取多个),返回 Contact 类型的对象
*
* Contact - 联系人对象
* 有一堆属性,看文档吧
*
*
* 注:
* 1、通过 ContactPicker 选取的联系人,可以获取的信息有限,但是可以通过 ContactStore 获取联系人的完整信息
* 2、通过 ContactStore 是可以获取到全部联系人的完整信息的,这部分知识点以后再写
* 3、通过 ContactStore 获取数据的话,需要在 Package.appxmanifest 中配置 <Capability Name = "contacts" />
*/ using System;
using System.Collections.Generic;
using Windows.ApplicationModel.Contacts;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging; namespace Windows10.Picker
{
public sealed partial class ContactPicker2 : Page
{
public ContactPicker2()
{
this.InitializeComponent();
} private async void btnPickContact_Click(object sender, RoutedEventArgs e)
{
ContactPicker contactPicker = new ContactPicker();
// 指定需要选取的联系人的字段
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email);
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber); // 启动联系人选取器,以选择一个联系人
Contact contact = await contactPicker.PickContactAsync(); if (contact != null)
{
lblMsg.Text += string.Format("name:{0}", contact.Name);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += string.Format("id:{0}", contact.Id);
lblMsg.Text += Environment.NewLine; foreach (ContactEmail email in contact.Emails)
{
lblMsg.Text += string.Format("email kind:{0}, email address:{1}", email.Kind, email.Address);
lblMsg.Text += Environment.NewLine;
} foreach (ContactPhone phone in contact.Phones)
{
lblMsg.Text += string.Format("phone kind:{0}, phone number:{1}, phone description:{2}", phone.Kind, phone.Number, phone.Description);
lblMsg.Text += Environment.NewLine;
} ContactStore contactStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);
// 通过 ContactStore 和联系人 id 可以获取到联系人的完整信息(需要配置 <Capability Name = "contacts" />)
Contact realContact = await contactStore.GetContactAsync(contact.Id); // 通过 ContactStore 也是可以拿到全部联系人信息的,这部分知识点以后再写
// IReadOnlyList<Contact> contacts = await contactStore.FindContactsAsync(); // 显示联系人的图片(只通过 ContactPicker 是获取不到的)
IRandomAccessStreamReference imageStreamRef = realContact.SmallDisplayPicture;
if (imageStreamRef != null)
{
IRandomAccessStream imageStream = await imageStreamRef.OpenReadAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream);
imgThumbnail.Source = bitmapImage;
}
}
else
{
lblMsg.Text += "取消了";
lblMsg.Text += Environment.NewLine;
}
}
}
}

OK
[源码下载]

背水一战 Windows 10 (96) - 选取器: ContactPicker的更多相关文章

  1. 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater

    [源码下载] 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater 作者:webabcd 介绍背水一战 Windows 10 之 选取器 CachedFileUp ...

  2. 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器

    [源码下载] 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件保存选取器 示例1.演示如何 ...

  3. 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器

    [源码下载] 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件打开选取器 示例1.演示如何 ...

  4. 背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker

    [源码下载] 背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker 作者:webabcd 介绍背水一战 Wi ...

  5. 背水一战 Windows 10 (27) - 控件(文本类): TextBlock

    [源码下载] 背水一战 Windows 10 (27) - 控件(文本类): TextBlock 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) TextBlock 示例 ...

  6. 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧

    [源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...

  7. 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue

    [源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...

  8. 背水一战 Windows 10 (9) - 资源: 资源限定符概述, 资源限定符示例

    [源码下载] 背水一战 Windows 10 (9) - 资源: 资源限定符概述, 资源限定符示例 作者:webabcd 介绍背水一战 Windows 10 之 资源 资源限定符概述 资源限定符示例 ...

  9. 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate

    [源码下载] 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate 作者:webabcd 介绍背水一战 Windows 10 ...

随机推荐

  1. python note 09 初识函数

    1.函数 def my_len(): #自定义函数(相当于len) i = 0 for k in s: i += 1 print(i) print(my_len()) #输出None,因为没有返回值 ...

  2. mysql数据库优化(转)

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...

  3. 跨DB Server创建View

    exec sp_configure 'show advanced options',1reconfigureexec sp_configure 'Ad Hoc Distributed Queries' ...

  4. 命令行连WiFi

    命令行连WiFi sudo iw dev wlan0 scan |grep SSID wpa_cli -iwlan0 add_network wpa_cli -iwlan0 set_network 4 ...

  5. Qt4.8.6开发WinCE 5.0环境搭建

    Qt-Wince5.0开发环境介绍 1.Windows7SP1 64 2.vs2008,以及sp1补丁 3.编译qt-everywhere-opensource-src-4.8.6.zip 4.qt- ...

  6. svn如何根据提交日志信息回退版本

    问题场景: 1 记得提交的日志信息中包含openssl,但是不记得这次提交的版本号revesion,是svn初始化后中间的某次提交: 2 svn环境的操作系统平台为Fedora, 即命令行下:而且sv ...

  7. 一张图说明TCP和UCP协议

    图片来自网络. 本来不想打字了,但是博客园有字数限制... 第一次 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SENT状态,等待服务器确认:SYN:同步序列编号( ...

  8. 201621123002《JAVA程序设计》第十四周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 用 ...

  9. 服务管理之rsync

    目录 rsync 1. rsync简介 2. rsync特性 4. rsync命令 5. rsync+inotify rsync 1. rsync简介 rsync是linux系统下的数据镜像备份工具. ...

  10. Context 解析

    ·  ContextWrapper比较有意思,其在SDK中的说明为“Proxying implementation ofContext that simply delegates all of its ...