選取器: 聯系人選取窗口, 自定義聯系人選取窗口
介紹
重新想象 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(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
<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 > 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
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar