與眾不同 windows phone (26) - Contacts and Calendar(聯絡人和日曆)
作者:webabcd
介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之聯絡人和日曆
- 獲取聯絡人相關資料
- 獲取日曆相關資料
示例
1、演示如何獲取聯絡人相關資料
ContactPictureConverter.cs
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.UserData; using System.IO; using Microsoft.Phone; namespace Demo.ContactsAndCalendar { public class ContactPictureConverter : System.Windows.Data.IValueConverter { // 提取 Contact 中的圖片,將圖片轉換成 WriteableBitmap 型別物件並返回 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Contact contact = value as Contact; if (contact == null) return null; // 將聯絡人圖片轉換成 WriteableBitmap 型別的物件,並返回此物件 Stream imageStream = contact.GetPicture(); if (imageStream != null) return PictureDecoder.DecodeJpeg(imageStream); return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }
ContactsDemo.xaml
<phone:PhoneApplicationPage x:Class="Demo.ContactsAndCalendar.ContactsDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True" xmlns:converter="clr-namespace:Demo.ContactsAndCalendar"> <phone:PhoneApplicationPage.Resources> <converter:ContactPictureConverter x:Key="ContactPictureConverter" /> </phone:PhoneApplicationPage.Resources> <Grid x:Name="LayoutRoot" Background="Transparent"> <StackPanel Orientation="Vertical"> <TextBlock Name="lblMsg" /> <Button Name="btnGetData" Content="獲取聯絡人相關資料" Click="btnGetData_Click" /> <!--用於繫結裝置中的全部聯絡人資訊,並顯示聯絡人的第一個 email 地址和第一個電話號碼--> <ListBox Name="listBoxContacts" ItemsSource="{Binding}" Height="200" Margin="0,15,0,0" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=EmailAddresses[0].EmailAddress, Mode=OneWay}" /> <TextBlock Text="{Binding Path=PhoneNumbers[0].PhoneNumber, Mode=OneWay}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <!--用於繫結裝置中聯絡人 email 地址帶“hotmail”的聯絡人資料,並顯示聯絡人的圖片及聯絡人的名稱--> <ListBox Name="listBoxContactsWithHotmail" ItemsSource="{Binding}" Height="200" Margin="0,15,0,0"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Border BorderThickness="2" HorizontalAlignment="Left" BorderBrush="{StaticResource PhoneAccentBrush}" > <Image Source="{Binding Converter={StaticResource ContactPictureConverter}}" Width="48" Height="48" Stretch="Fill" /> </Border> <TextBlock Text="{Binding Path=DisplayName, Mode=OneWay}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Grid> </phone:PhoneApplicationPage>
ContactsDemo.xaml.cs
/* * 演示如何獲取裝置中的聯絡人資料 * * Contacts - 用於獲取聯絡人資料的類 * Accounts - 聯絡人資料可能來自使用者的不同帳戶,Accounts 就是用來獲取這個不同賬戶的,即資料來源集合(只讀屬性,返回 Account 物件的集合) * SearchAsync(string filter, FilterKind filterKind, object state) - 開始非同步搜尋聯絡人資料 * string filter - 篩選關鍵字 * 當 filterKind 設定為 DisplayName, EmailAddress, PhoneNumber 時指定篩選關鍵字 * 當 filterKind 設定為 None, PinnedToStart 時此值無用,直接寫 String.Empty 就好 * FilterKind filterKind - 篩選器的類別(Microsoft.Phone.UserData.FilterKind 列舉) * FilterKind.None - 返回全部聯絡人資料 * FilterKind.PinnedToStart - 返回已固定到開始螢幕的聯絡人資料 * FilterKind.DisplayName - 按名稱搜尋 * FilterKind.EmailAddress - 按 email 地址搜尋 * FilterKind.PhoneNumber - 按電話號碼搜尋 * object state - 非同步過程中的上下文 * SearchCompleted - 搜尋完成時所觸發的事件(事件引數 ContactsSearchEventArgs) * * ContactsSearchEventArgs * Filter - 篩選關鍵字 * FilterKind - 篩選器的類別 * Results - 返回搜尋結果,Contact 物件的集合 * State - 非同步過程中的上下文 * * Contact - 聯絡人 * Accounts - 與此聯絡人關聯的資料來源集合 * Addresses - 與此聯絡人關聯的地址資料集合 * Birthdays - 與此聯絡人關聯的生日資料集合 * Children - 子女 * Companies - 公司 * CompleteName - 聯絡人全稱(包含諸如名字、職稱和暱稱之類的資訊) * DisplayName - 顯示名稱 * EmailAddresses - email 地址 * IsPinnedToStart - 是否固定到了開始螢幕 * Notes - 備註 * PhoneNumbers - 電話號碼 * SignificantOthers - 與此聯絡人關聯的重要他人 * Websites - 網站 * * Account - 賬戶 * Name - 賬戶名稱 * Kind - 賬戶的種類(Microsoft.Phone.UserData.StorageKind 列舉) * Phone, WindowsLive, Outlook, Facebook, Other */ using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using Microsoft.Phone.UserData; namespace Demo.ContactsAndCalendar { public partial class ContactsDemo : PhoneApplicationPage { public ContactsDemo() { InitializeComponent(); } private void btnGetData_Click(object sender, RoutedEventArgs e) { // 例項化 Contacts,註冊相關事件 Contacts contacts = new Contacts(); contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted); // 指定搜尋內容及搜尋方式,開始非同步搜尋聯絡人資訊 contacts.SearchAsync(String.Empty, FilterKind.None, null); lblMsg.Text = "資料載入中,請稍後。。。"; btnGetData.IsEnabled = false; } void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e) { try { // 繫結聯絡人資料 listBoxContacts.DataContext = e.Results; // 繫結聯絡人 email 地址帶“hotmail”的聯絡人資料 var hotmailContacts = from c in e.Results from ContactEmailAddress a in c.EmailAddresses where a.EmailAddress.Contains("hotmail") select c; listBoxContactsWithHotmail.DataContext = hotmailContacts; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } this.Dispatcher.BeginInvoke(delegate { lblMsg.Text = "資料載入完畢,共有聯絡人資料 " + e.Results.Count().ToString() + " 條"; btnGetData.IsEnabled = true; }); } } }
2、演示如何獲取日曆相關資料
CalendarDemo.xaml
<phone:PhoneApplicationPage x:Class="Demo.ContactsAndCalendar.CalendarDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent"> <StackPanel Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > <TextBlock Name="lblMsg" /> <Button Name="btnGetData" Content="獲取日曆相關資料" Click="btnGetData_Click" /> <!--用於繫結日曆資料,並顯示約會主題--> <ListBox Name="listBoxCalendar" ItemsSource="{Binding}" Height="200" Margin="0,15,0,0" > <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Subject, Mode=OneWay}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <!--用於繫結日曆資料(只繫結主日曆資料),並顯示約會主題--> <ListBox Name="listBoxCalendarPrimary" ItemsSource="{Binding}" Height="200" Margin="0,15,0,0" > <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Subject, Mode=OneWay}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Grid> </phone:PhoneApplicationPage>
CalendarDemo.xaml.cs
/* * 演示如何獲取裝置中的日曆資料 * * Appointments - 用於獲取日曆約會資料的類 * Accounts - 日曆約會資料可能來自使用者的不同帳戶,Accounts 就是用來獲取這個不同賬戶的,即資料來源集合(只讀屬性,返回 Account 物件的集合) * SearchAsync(DateTime startTimeInclusive, DateTime endTimeInclusive, int maximumItems, Account account, Object state) - 開始非同步搜尋日曆約會資料 * startTimeInclusive - 指定搜尋範圍:開始時間 * endTimeInclusive - 指定搜尋範圍:結束時間 * maximumItems - 指定返回約會資料的最大數量 * account - 指定約會資料的來源賬戶(不指定的話則全賬戶搜尋) * state - 非同步過程中的上下文 * SearchCompleted - 搜尋完成時所觸發的事件(事件引數 AppointmentsSearchEventArgs) * * AppointmentsSearchEventArgs * StartTimeInclusive - 搜尋範圍的開始時間 * EndTimeInclusive - 搜尋範圍的結束時間 * Results - 返回搜尋結果,Appointment 物件的集合 * State - 非同步過程中的上下文 * * Appointment - 約會 * Account - 與此約會關聯的資料來源 * Attendees - 與此約會關聯的參與者 * Details - 詳細說明 * StartTime - 約會的開始時間 * EndTime - 約會的結束時間 * IsAllDayEvent - 約會是否來自附加日曆 * IsPrivate - 是否是私人約會 * Location - 約會的地點 * Organizer - 約會的組織者 * Status - 如何處理此約會(Microsoft.Phone.UserData.AppointmentStatus 列舉) * Free - 空閒 * Tentative - 待定 * Busy - 忙碌 * OutOfOffice - 外出 * Subject - 約會的主題 */ using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using Microsoft.Phone.UserData; namespace Demo.ContactsAndCalendar { public partial class CalendarDemo : PhoneApplicationPage { public CalendarDemo() { InitializeComponent(); } private void btnGetData_Click(object sender, RoutedEventArgs e) { // 例項化 Appointments,並註冊相關事件 Appointments appointments = new Appointments(); appointments.SearchCompleted += new EventHandler<AppointmentsSearchEventArgs>(appointments_SearchCompleted); DateTime start = DateTime.Now; DateTime end = start.AddDays(7); int max = 20; // 指定搜尋範圍,開始非同步搜尋日曆資料 appointments.SearchAsync(start, end, max, null); lblMsg.Text = "資料載入中,請稍後。。。"; btnGetData.IsEnabled = false; } void appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e) { try { // 繫結所有日曆的資料 listBoxCalendar.DataContext = e.Results; // 只繫結主日曆資料,其他附加日曆的資料都過濾掉 var primaryCalendar = from Appointment a in e.Results where a.IsAllDayEvent == false select a; listBoxCalendarPrimary.DataContext = primaryCalendar; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } this.Dispatcher.BeginInvoke(delegate { btnGetData.IsEnabled = true; lblMsg.Text = "資料載入完畢"; }); } } }
OK
[原始碼下載]