與眾不同 windows phone (26) - Contacts and Calendar(聯系人和日歷)


[索引頁]
[源碼下載]


與眾不同 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
[源碼下載]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM