( 1 )XAML界面
(2)添加命名空间
using Microsoft.Phone.Tasks; |
(3)定义Chooser对象
PhoneNumberChooserTask phoneNumberChooser;//PhoneNumberChooserTask允许应用程序启动“联系人”应用程序。该方法可获取用户选择的联系人的电话号码
EmailAddressChooserTask emailAddressChooser;//EmailAddressChooserTask 允许应用程序启动“联系人”应用程序。该方法可获取用户选择的联系人的电子邮件地址
SavePhoneNumberTask phoneNumberSaver;//SavePhoneNumberTask 允许应用程序启动“联系人”应用程序。该方法允许用户将应用程序中的电话号码保存到新的或现有的联系人
SaveEmailAddressTask emailAddressSaver;//SaveEmailAddressTask 允许应用程序启动“联系人”应用程序。该方法允许用户将应用程序中的电子邮件地址保存到新的或现有的联系人 |
(4)实例化Chooser对象
phoneNumberChooser = new PhoneNumberChooserTask(); emailAddressChooser = new EmailAddressChooserTask(); phoneNumberSaver = new SavePhoneNumberTask(); emailAddressSaver = new SaveEmailAddressTask(); |
(5)为每一个Chooser指定Completed事件处理程序
phoneNumberChooser.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooser_Completed); emailAddressChooser.Completed += new EventHandler<EmailResult>(emailAddressChooser_Completed); phoneNumberSaver.Completed += new EventHandler<TaskEventArgs>(phoneNumberSaver_Completed); emailAddressSaver.Completed += new EventHandler<TaskEventArgs>(emailAddressSaver_Completed); |
(6)实现每一个事件处理程序
void emailAddressSaver_Completed(object sender, TaskEventArgs e) { //在任务正常完成的情况下,才可以提取返回数据,否则会引发异常。 if (e.TaskResult == TaskResult.OK) { MessageBox.Show("保存成功"); } } void phoneNumberSaver_Completed(object sender, TaskEventArgs e) { if (e.TaskResult == TaskResult.OK) { MessageBox.Show("保存成功"); } } void emailAddressChooser_Completed(object sender, EmailResult e) { if (e.TaskResult == TaskResult.OK) { txbPhoneOrEmail.Text = e.Email; } }
void phoneNumberChooser_Completed(object sender, PhoneNumberResult e) { if (e.TaskResult == TaskResult.OK) { txbPhoneOrEmail.Text = e.PhoneNumber; } } |
(7)定义按钮事件
private void btnInvokeChooser_Click(object sender, RoutedEventArgs e) { if (rbPhoneNumberChooser.IsChecked.Value) { phoneNumberChooser.Show(); } if (rbEmaiAddresslChooser.IsChecked.Value) { emailAddressChooser.Show(); } if (rbPhoneNumberSaver.IsChecked.Value) { phoneNumberSaver.PhoneNumber = txbPhoneOrEmail.Text.Trim(); phoneNumberSaver.Show(); } if (rbEmailAddressSaver.IsChecked.Value) { emailAddressSaver.Email = txbPhoneOrEmail.Text.Trim(); emailAddressSaver.Show(); } } |