[源碼下載]
html
做者:webabcd
介紹
背水一戰 Windows 10 之 選取器html5
示例
一、演示如何經過 ContactPicker 選擇一個或多個聯繫人
Picker/ContactPickerDemo.xamlc++
<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.csweb
/* 演示如何經過 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 > 0) { 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; } } } }
二、演示如何經過 ContactPicker 選取聯繫人,並獲取其完整信息
Picker/ContactPicker2.xamlexpress
<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.cswindows
/* 演示如何經過 ContactPicker 選取聯繫人,並獲取其完整信息 * * ContactPicker - 聯繫人選擇窗口(有好多 api 在 uwp 中廢棄了) * DesiredFieldsWithContactFieldType - 須要獲取的聯繫人的字段(能夠添加 ContactFieldType 類型的枚舉) * PickContactAsync() - 彈出聯繫人選取器(只能選取一個),返回 Contact 類型的對象 * PickContactsAsync() - 彈出聯繫人選取器(能夠選取多個),返回 Contact 類型的對象 * * Contact - 聯繫人對象 * 有一堆屬性,看文檔吧 * * * 注: * 一、經過 ContactPicker 選取的聯繫人,能夠獲取的信息有限,可是能夠經過 ContactStore 獲取聯繫人的完整信息 * 二、經過 ContactStore 是能夠獲取到所有聯繫人的完整信息的,這部分知識點之後再寫 * 三、經過 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
[源碼下載]api