原文:与众不同 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();
int max = ; // 指定搜索范围,开始异步搜索日历数据
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
[源码下载]

与众不同 windows phone (26) - Contacts and Calendar(联系人和日历)的更多相关文章

  1. 与众不同 windows phone (39) - 8.0 联系人和日历

    [源码下载] 与众不同 windows phone (39) - 8.0 联系人和日历 作者:webabcd 介绍与众不同 windows phone 8.0 之 联系人和日历 自定义联系人存储的增删 ...

  2. 与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展

    [源码下载] 与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展 作者:webabcd 介绍与众不同 windows ph ...

  3. 与众不同 windows phone (3) - Application Bar(应用程序栏)

    原文:与众不同 windows phone (3) - Application Bar(应用程序栏) [索引页][源码下载] 与众不同 windows phone (3) - Application ...

  4. 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null

    [源码下载] 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null 作者:webabcd 介绍背水一战 Windows 10 之 XAML ...

  5. 与众不同 windows phone 8.0 & 8.1 系列文章索引

    [源码下载] [与众不同 windows phone 7.5 (sdk 7.1) 系列文章索引] 与众不同 windows phone 8.0 & 8.1 系列文章索引 作者:webabcd ...

  6. 与众不同 windows phone (34) - 8.0 新的控件: LongListSelector

    [源码下载] 与众不同 windows phone (34) - 8.0 新的控件: LongListSelector 作者:webabcd 介绍与众不同 windows phone 8.0 之 新的 ...

  7. 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirectionsTask, MapDownloaderTask

    [源码下载] 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirec ...

  8. 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile

    [源码下载] 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile 作者:webabcd 介绍与众不同 windows ...

  9. 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件

    [源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...

随机推荐

  1. javascript每日一练(九)——运动一:匀速运动

    一.js的运动 匀速运动 清除定时器 开启定时器 运动是否完成:a.运动完成,清除定时器:b.运动未完成继续 匀速运动停止条件:距离足够近  Math.abs(当然距离-目标距离) < 最小运动 ...

  2. Qt国际化(Q_DECLARE_TR_FUNCTIONS() 宏给非Qt类添加翻译支持,以前没见过QTextEncoder和QTextDecoder和QLibraryInfo::location()和QEvent::LanguageChange)

    Internationalization with Qt 应用程序的国际化就是使得程序能在国际间可用而不仅仅是在本国可用的过程. Relevant Qt Classes andAPIs 以下的类支持Q ...

  3. QList 和std::list的比较

    QList QList<T> 是一个Qt通用容器类.它存储一序列的值,并且提供基于索引的数据访问方法和快速的插入和删除操作. QList<T>, QLinkedList< ...

  4. MinGW开发工具的安装

    MinGW是Minimalist GNU for Windows的缩写,是把linux下的GNU开发工具包移植到windows的项目之一.和Cygwin不一样的是,MinGW不提供linux的posi ...

  5. hdu4717 The Moving Points(二分做法)

    这道题看了大家都是用三分做的,其实这道题也是可以用二分来做的,就是利用一下他们的单调性. 对于N个点,总共要考虑N(N+1)/2个距离,距离可以用二次函数表示,而且开口都是向上的. 下面具体说一下二分 ...

  6. STL之iterator(迭代器)

    3.迭代器简单介绍 除了使用下标来訪问vector对象的元素外,标准库还提供了訪问元素的方法:使用迭代器.迭代器是一种检查容器内元素而且遍历元素的数据类型. 百科释义: 迭代器(iterator)是一 ...

  7. MSSQL - SqlDataReader

    DataReader对象: ·DataReader对象是一个读取行的只读流的方式,绑定数据时比使用数据集方式性能要高,因为他是只读的,所以如果要对数据库中的数据进行修改就需要借助 将所做的修改保存到数 ...

  8. Windows NT 技术简介

    Windows NT 技术简介 NT:New Technoly(新技术,因比DOS.WIN9X采用了很多新技术而得名) Windows NT基本介绍 WindowsNT是Microsoft推出的面向工 ...

  9. 《火球——UML大战需求分析》(第1章 大话UML)——1.5 小结和练习

    说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...

  10. Android 计时与倒计时

    方法一 Timer与TimerTask(Java实现) [java]  view plain copy print ?   public class timerTask extends Activit ...