突出 windows phone (5) – Chooser(选择器)与众不同 windows phone (26) – Contacts and Calendar(联系人和日历)

[索引页]
[源码下载]

[索引页]
[源码下载]

特 windows phone (5) – Chooser(选择器)

奇 windows phone (26) – Contacts and Calendar(联系人和日历)

作者:webabcd

作者:webabcd

介绍
新鲜 windows phone 7.5 (sdk 7.1) 之选择器

介绍
破例 windows phone 7.5 (sdk 7.1) 之联系人和日历

  • AddressChooserTask – 选择联系人的地方
  • EmailAddressChooserTask – 选择联系人之email
  • PhoneNumberChooserTask – 选择联系人的电话
  • SaveContactTask – 保存联系方式
  • SaveEmailAddressTask – 保存电子邮件地址
  • SavePhoneNumberTask – 保存电话号码
  • SaveRingtoneTask – 保存铃声
  • PhotoChooserTask – 从图库里选择像
  • CameraCaptureTask – 相机拍摄
  • GameInviteTask – 游戏邀请(邀请其他人加入自己的玩乐会话)
  • 落联系人相关数据
  • 取得日历相关数据

示例
1、AddressChooserTask 的 Demo
AddressChooserTask.xaml.cs

示例
1、演示如何得到联系人相关数据
ContactPictureConverter.cs

/*
 * AddressChooserTask - 选择联系人的地址
 *     Completed - 选中地址之后所执行的事件
 *     Show() - 显示联系人列表
 * 
 * TaskResult - 用户选择后的结果
 *     OK - 成功
 *     Cancel - 取消
 *     None - 失败
 *     
 * AddressResult - Completed 事件的事件参数
 *     DisplayName - 联系人名称
 *     Address - 联系人的地址
 */

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.Tasks;

namespace Demo.Choosers
{
    public partial class AddressChooserTaskDemo : PhoneApplicationPage
    {
        private AddressChooserTask _addressChooserTask;

        public AddressChooserTaskDemo()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            _addressChooserTask = new AddressChooserTask();
            _addressChooserTask.Completed += new EventHandler<AddressResult>(_addressChooserTask_Completed);

            try
            {
                _addressChooserTask.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void _addressChooserTask_Completed(object sender, AddressResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                MessageBox.Show("“" + e.DisplayName + "”的地址是:" + e.Address);
            }
        }
    }
}
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

2、EmailAddressChooserTask 的 Demo
EmailAddressChooserTask.xaml.cs

<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>
/*
 * EmailAddressChooserTask - 选择联系人的email
 *     Completed - 选中email之后所执行的事件
 *     Show() - 显示联系人列表
 *     
 * TaskResult - 用户选择后的结果
 *     OK - 成功
 *     Cancel - 取消
 *     None - 失败
 *     
 * EmailResult - Completed 事件的事件参数
 *     DisplayName - 联系人名称
 *     Email - 联系人的email
 */

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.Tasks;

namespace Demo.Choosers
{
    public partial class EmailAddressChooserTaskDemo : PhoneApplicationPage
    {
        private EmailAddressChooserTask _emailAddressChooserTask;

        public EmailAddressChooserTaskDemo()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            _emailAddressChooserTask = new EmailAddressChooserTask();
            _emailAddressChooserTask.Completed += new EventHandler<EmailResult>(_emailAddressChooserTask_Completed);

            try
            {
                _emailAddressChooserTask.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void _emailAddressChooserTask_Completed(object sender, EmailResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                MessageBox.Show("“" + e.DisplayName + "”的email是:" + e.Email);
            }
        }
    }
}

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;
            });
        }
    }
}

3、PhoneNumberChooserTask 的 Demo
PhoneNumberChooserTask.xaml.cs

2、演示如何赢得日历相关数据
CalendarDemo.xaml

/*
 * PhoneNumberChooserTask - 选择联系人的电话
 *     Completed - 选中电话之后所执行的事件
 *     Show() - 显示联系人列表
 *     
 * TaskResult - 用户选择后的结果
 *     OK - 成功
 *     Cancel - 取消
 *     None - 失败
 *     
 * PhoneNumberResult - Completed 事件的事件参数
 *     DisplayName - 联系人名称
 *     PhoneNumber - 联系人的电话
 */

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.Tasks;

namespace Demo.Choosers
{
    public partial class PhoneNumberChooserTaskDemo : PhoneApplicationPage
    {
        private PhoneNumberChooserTask _phoneNumberChooserTask;

        public PhoneNumberChooserTaskDemo()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            _phoneNumberChooserTask = new PhoneNumberChooserTask();
            _phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(_phoneNumberChooserTask_Completed);

            try
            {
                _phoneNumberChooserTask.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void _phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                MessageBox.Show("“" + e.DisplayName + "”的电话号码是:" + e.PhoneNumber);
            }
        }
    }
}
<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

4、SaveContactTask 的 Demo
SaveContactTask.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 = "数据加载完毕";
            });
        }
    }
}
/*
 * SaveContactTask - 保存联系方式
 *     Completed - 完成保存操作(成功、取消、失败)之后所执行的事件
 *     FirstName, MiddleName, LastName, Nickname, HomeAddressCity, HomeAddressCountry, HomeAddressState, HomeAddressStreet, HomeAddressZipCode, HomePhone, WorkAddressCity, WorkAddressCountry, WorkAddressState, WorkAddressStreet, WorkAddressZipCode, WorkEmail, WorkPhone, Suffix, Title, Website, JobTitle, Company, MobilePhone, PersonalEmail, OtherEmail, Notes
 *     Show() - 显示用于保存联系方式的窗口
 * 
 * TaskResult - 任务结果
 *     OK - 成功
 *     Cancel - 取消
 *     None - 失败
 */

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.Tasks;

namespace Demo.Choosers
{
    public partial class SaveContactTaskDemo : PhoneApplicationPage
    {
        private SaveContactTask _saveContactTask;

        public SaveContactTaskDemo()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            _saveContactTask = new SaveContactTask();
            _saveContactTask.Completed += new EventHandler<SaveContactResult>(_saveContactTask_Completed);

            try
            {
                _saveContactTask.FirstName = "web";
                _saveContactTask.LastName = "abcd";
                _saveContactTask.MobilePhone = "11111111111";

                _saveContactTask.Show();
            }
            catch (System.InvalidOperationException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void _saveContactTask_Completed(object sender, SaveContactResult e)
        {
            switch (e.TaskResult)
            {
                case TaskResult.OK:
                    MessageBox.Show("保存成功");
                    break;

                case TaskResult.Cancel:
                    MessageBox.Show("取消保存");
                    break;

                case TaskResult.None:
                    MessageBox.Show("无法保存");
                    break;
            }
        }
    }
}

OK
[源码下载]

 

5、SaveEmailAddressTask 的 Demo
SaveEmailAddressTask.xaml.cs

/*
 * SaveEmailAddressTask - 保存电子邮件地址
 *     Completed - 完成保存操作(成功、取消、失败)之后所执行的事件
 *     Email - 需要保存的电子邮件地址
 *     Show() - 显示用于保存电子邮件地址的窗口
 * 
 * TaskResult - 任务结果
 *     OK - 成功
 *     Cancel - 取消
 *     None - 失败
 */

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.Tasks;

namespace Demo.Choosers
{
    public partial class SaveEmailAddressTaskDemo : PhoneApplicationPage
    {
        private SaveEmailAddressTask _saveEmailAddressTask;

        public SaveEmailAddressTaskDemo()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            _saveEmailAddressTask = new SaveEmailAddressTask();
            _saveEmailAddressTask.Completed += new EventHandler<TaskEventArgs>(_saveEmailAddressTask_Completed);

            try
            {
                _saveEmailAddressTask.Email = "a@b.c";

                _saveEmailAddressTask.Show();
            }
            catch (System.InvalidOperationException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void _saveEmailAddressTask_Completed(object sender, TaskEventArgs e)
        {
            switch (e.TaskResult)
            {
                case TaskResult.OK:
                    MessageBox.Show("保存成功");
                    break;

                case TaskResult.Cancel:
                    MessageBox.Show("取消保存");
                    break;

                case TaskResult.None:
                    MessageBox.Show("无法保存");
                    break;
            }
        }
    }
}

 

6、SavePhoneNumberTask 的 Demo
SavePhoneNumberTask.xaml.cs

/*
 * SavePhoneNumberTask - 保存电话号码
 *     Completed - 完成保存操作(成功、取消、失败)之后所执行的事件
 *     PhoneNumber - 需要保存的电话号码
 *     Show() - 显示用于保存电话号码的窗口
 * 
 * TaskResult - 任务结果
 *     OK - 成功
 *     Cancel - 取消
 *     None - 失败
 */

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.Tasks;

namespace Demo.Choosers
{
    public partial class SavePhoneNumberTaskDemo : PhoneApplicationPage
    {
        private SavePhoneNumberTask _savePhoneNumberTask;

        public SavePhoneNumberTaskDemo()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            _savePhoneNumberTask = new SavePhoneNumberTask();
            _savePhoneNumberTask.Completed += new EventHandler<TaskEventArgs>(_savePhoneNumberTask_Completed);

            try
            {
                _savePhoneNumberTask.PhoneNumber = "11111111111";

                _savePhoneNumberTask.Show();
            }
            catch (System.InvalidOperationException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void _savePhoneNumberTask_Completed(object sender, TaskEventArgs e)
        {
            switch (e.TaskResult)
            {
                case TaskResult.OK:
                    MessageBox.Show("保存成功");
                    break;

                case TaskResult.Cancel:
                    MessageBox.Show("取消保存");
                    break;

                case TaskResult.None:
                    MessageBox.Show("无法保存");
                    break;
            }
        }
    }
}

 

7、SaveRingtoneTask 的 Demo
SaveRingtoneTask.xaml.cs

/*
 * SaveRingtoneTask - 保存铃声
 *     Completed - 保存铃声之后所执行的事件
 *     Source - 铃声文件的地址
 *     IsShareable - 指示该铃声是否可被其他应用程序访问
 *     DisplayName - 铃声的名称(显示用)
 *     Show() - 显示保存铃声窗口
 * 
 * TaskResult - 任务结果
 *     OK - 成功
 *     Cancel - 取消
 *     None - 失败
 */

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.Tasks;

namespace Demo.Choosers
{
    public partial class SaveRingtoneTaskDemo : PhoneApplicationPage
    {
        private SaveRingtoneTask _saveRingtoneChooser;

        public SaveRingtoneTaskDemo()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            _saveRingtoneChooser = new SaveRingtoneTask();
            _saveRingtoneChooser.Completed += new EventHandler<TaskEventArgs>(_saveRingtoneChooser_Completed);

            try
            {
                _saveRingtoneChooser.Source = new Uri("appdata:/Assets/SuperMario.mp3"); // appdata: 代表程序包内
                // saveRingtoneChooser.Source = new Uri("isostore:/myTone.mp3"); // isostore: 代表独立存储
                // _saveRingtoneChooser.Source = new Uri("http://a.b.c/d.mp3"); // 网络地址
                _saveRingtoneChooser.IsShareable = true;

                _saveRingtoneChooser.DisplayName = "超级马里奥铃声";

                _saveRingtoneChooser.Show();
            }
            catch (System.InvalidOperationException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void _saveRingtoneChooser_Completed(object sender, TaskEventArgs e)
        {
            switch (e.TaskResult)
            {
                case TaskResult.OK:
                    MessageBox.Show("保存成功");
                    break;

                case TaskResult.Cancel:
                    MessageBox.Show("取消保存");
                    break;

                case TaskResult.None:
                    MessageBox.Show("无法保存");
                    break;
            }
        }
    }
}

 

8、PhotoChooserTask 的 Demo
PhotoChooserTask.xaml.cs

/*
 * PhotoChooserTask - 从图库里选择照片
 *     Completed - 选中照片之后所执行的事件
 *     Show() - 显示图库
 * 
 * TaskResult - 任务结果
 *     OK - 成功
 *     Cancel - 取消
 *     None - 失败
 *     
 * PhotoResult - Completed 事件的事件参数
 *     ChosenPhoto - 照片数据(System.IO.Stream 类型)
 *     OriginalFileName - 照片的文件名
 */

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.Tasks;

namespace Demo.Choosers
{
    public partial class PhotoChooserTaskDemo : PhoneApplicationPage
    {
        private PhotoChooserTask _photoChooserTask;

        public PhotoChooserTaskDemo()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            _photoChooserTask = new PhotoChooserTask();
            _photoChooserTask.Completed += new EventHandler<PhotoResult>(_photoChooserTask_Completed);

            try
            {
                _photoChooserTask.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void _photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
                bmp.SetSource(e.ChosenPhoto);
                img.Source = bmp;
            }
        }
    }
}

 

9、CameraCaptureTask 的 Demo
CameraCaptureTask.xaml.cs

/*
 * CameraCaptureTask - 相机拍照
 *     Completed - 拍照并确认之后所执行的事件
 *     Show() - 启动相机
 * 
 * TaskResult - 任务结果
 *     OK - 成功
 *     Cancel - 取消
 *     None - 失败
 *     
 * PhotoResult - Completed 事件的事件参数
 *     ChosenPhoto - 照片数据(System.IO.Stream 类型)
 *     OriginalFileName - 照片的文件名
 */

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.Tasks;

namespace Demo.Choosers
{
    public partial class CameraCaptureTaskDemo : PhoneApplicationPage
    {
        private CameraCaptureTask _cameraCaptureTask;

        public CameraCaptureTaskDemo()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            _cameraCaptureTask = new CameraCaptureTask();
            _cameraCaptureTask.Completed += new EventHandler<PhotoResult>(_cameraCaptureTask_Completed);

            try
            {
                _cameraCaptureTask.Show();
            }
            catch (System.InvalidOperationException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void _cameraCaptureTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
                bmp.SetSource(e.ChosenPhoto);
                img.Source = bmp;
            }
        }
    }
}

 

10、GameInviteTask 的 Demo
GameInviteTask.xaml.cs

/*
 * GameInviteTask - 游戏邀请(邀请其他人加入我的游戏会话)
 *     Completed - 完成操作之后所执行的事件
 *     SessionId - 游戏的会话 ID
 *     Show() - 发出邀请
 *     
 * TaskResult - 用户选择后的结果
 *     OK - 成功
 *     Cancel - 取消
 *     None - 失败
 */

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.Tasks;

namespace Demo.Choosers
{
    public partial class GameInviteTaskDemo : PhoneApplicationPage
    {
        private GameInviteTask _gameInviteTask;

        public GameInviteTaskDemo()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            _gameInviteTask = new GameInviteTask();
            _gameInviteTask.Completed += new EventHandler<TaskEventArgs>(_gameInviteTask_Completed);

            try
            {
                // 游戏需要在 Windows Phone 上的 Xbox LIVE 中发布,并且邀请人也要是 Xbox LIVE 的会员
                _gameInviteTask.SessionId = "游戏的 session id";
                _gameInviteTask.Show();
            }
            catch (System.InvalidOperationException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void _gameInviteTask_Completed(object sender, TaskEventArgs e)
        {
            switch (e.TaskResult)
            {
                case TaskResult.OK:
                    MessageBox.Show("游戏邀请已发送");
                    break;

                case TaskResult.Cancel:
                    MessageBox.Show("游戏邀请已取消");
                    break;

                case TaskResult.None:
                    MessageBox.Show("游戏邀请无法发送");
                    break;
            }
        }
    }
}

OK
[源码下载]

相关文章