原文:与众不同 windows phone (5) - Chooser(选择器)
[索引页]
[源码下载]
与众不同 windows phone (5) - Chooser(选择器)
作者:webabcd
介绍
与众不同 windows phone 7.5 (sdk 7.1) 之选择器
- AddressChooserTask - 选择联系人的地址
- EmailAddressChooserTask - 选择联系人的email
- PhoneNumberChooserTask - 选择联系人的电话
- SaveContactTask - 保存联系方式
- SaveEmailAddressTask - 保存电子邮件地址
- SavePhoneNumberTask - 保存电话号码
- SaveRingtoneTask - 保存铃声
- PhotoChooserTask - 从图库里选择照片
- CameraCaptureTask - 相机拍照
- GameInviteTask - 游戏邀请(邀请其他人加入我的游戏会话)
示例
1、AddressChooserTask 的 Demo
AddressChooserTask.xaml.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); } } } }
2、EmailAddressChooserTask 的 Demo
EmailAddressChooserTask.xaml.cs
/* * 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); } } } }
3、PhoneNumberChooserTask 的 Demo
PhoneNumberChooserTask.xaml.cs
/* * 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); } } } }
4、SaveContactTask 的 Demo
SaveContactTask.xaml.cs
/* * 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; } } } }
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
[源码下载]
时间: 2024-10-01 00:25:15