关闭

启动器与选择器-常用Task

标签: buttonobjectshell任务webbrowserimage
315人阅读 评论(0) 收藏 举报
分类:

 Windows Phone 7没有提供直接操作SMS、Phone、Email、Camera等的API,通过调用Task来调用系统的相关应用来拨打电话、发送短信、保存联系人、拍照等,当Task启动后,自己的应用程序就会被终止(暂停)通过Tombstone-墓碑机制来再次唤醒自己的程序,并需要维护相关的状态。

      我们首先从Microsoft.Phone.Tasks 命名空间说起,该命名空间允许应用程序使用启动器和选择器为其用户提供一组常见的任务,如打电话发送电子邮件以及拍摄照片等。

      基本知识:

     10.0版本的VS的Microsoft.Phone.Tasks 命名空间如下:

      相关的说明如下:

      所有的Task都有Show方法,调用Show方法启动任务,任务执行完毕一般会返回应用。所有任务都不是自动启动,而是需要用户手动点击来启动,是WP7基于安全考虑,防止泄密、吸费等问题。无法用来实现批量短信。所有Task都需要用户手动触发执行。

      还有一些Task需要得到执行结果的任务,这些任务一般都是从ChooseBase继承,监听Completed事件,从e中可以获得执行结果,所有e任务都有属性TaskResult表示执行结果(OK执行成功,Cancel任务被取消),Error表示执行过程中出现异常信息。

      下面附上示例性的代码用来说明使用方法,MainPage.xaml的代码如下:

<phone:PhoneApplicationPage 
x:Class="Task.MainPage"
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"
mc:Ignorable
="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily
="{StaticResource PhoneFontFamilyNormal}"
FontSize
="{StaticResource PhoneFontSizeNormal}"
Foreground
="{StaticResource PhoneForegroundBrush}"
SupportedOrientations
="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible
="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="http://www.cnblogs.com/DebugLZQ" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="常用Task" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="发短信" Height="72" HorizontalAlignment="Left" Margin="33,35,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click"/>
<Button Content="发邮件" Height="72" HorizontalAlignment="Left" Margin="33,113,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
<Button Content="打电话" Height="72" HorizontalAlignment="Left" Margin="33,283,0,0" Name="button3" VerticalAlignment="Top" Width="160" Click="button3_Click" />
<Button Content="打开网页" Height="72" HorizontalAlignment="Left" Margin="33,205,0,0" Name="button4" VerticalAlignment="Top" Width="160" Click="button4_Click" />
<Button Content="保存号码" Height="72" HorizontalAlignment="Left" Margin="33,361,0,0" Name="button5" VerticalAlignment="Top" Width="160" Click="button5_Click" />
<Button Content="选择号码" Height="72" HorizontalAlignment="Left" Margin="33,439,0,0" Name="button6" VerticalAlignment="Top" Width="160" Click="button6_Click" />
<Button Content="拍照" Height="72" HorizontalAlignment="Left" Margin="245,35,0,0" Name="button7" VerticalAlignment="Top" Width="160" Click="button7_Click" />
<Image Height="150" HorizontalAlignment="Left" Margin="232,113,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" />
<Button Content="选择图片" Height="72" HorizontalAlignment="Left" Margin="245,283,0,0" Name="button8" VerticalAlignment="Top" Width="160" Click="button8_Click" />
</Grid>
</Grid>

<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
-->

</phone:PhoneApplicationPage>

      MainPage.xaml.cs的代码如下:

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;
using System.Windows.Media.Imaging;

namespace Task
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
//发短信
//SmsComposeTask smsComseTask = new SmsComposeTask();
//smsComseTask.To = "10086";
//smsComseTask.Body = "CXHF";
//smsComseTask.Show();
SmsComposeTask sms = new SmsComposeTask()
{
To="10086",
Body="CXHF"
};
sms.Show();
}

private void button2_Click(object sender, RoutedEventArgs e)
{
EmailComposeTask email = new EmailComposeTask()
{
Subject="关于晚上吃饭",
Body="晚上在那里吃饭啊?",
Cc="",
To="xxxxxx@qq.com"
};
email.Show();
}

private void button4_Click(object sender, RoutedEventArgs e)
{
WebBrowserTask webBrowser = new WebBrowserTask()
{
URL="http://www.cnblogs/DebugLZQ"
};
webBrowser.Show();
}

private void button3_Click(object sender, RoutedEventArgs e)
{
PhoneCallTask phone = new PhoneCallTask()
{
DisplayName="中国移动",
PhoneNumber="10086"
};
phone.Show();//
}

/// <summary>
/// 需要监听事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, RoutedEventArgs e)
{
SavePhoneNumberTask savePhoneNumber = new SavePhoneNumberTask() {
PhoneNumber="10086"
};
savePhoneNumber.Completed += new EventHandler<TaskEventArgs>(savePhoneNumber_Completed);
savePhoneNumber.Show();
}

void savePhoneNumber_Completed(object sender, TaskEventArgs e)
{
if (e.Error ==null&&e.TaskResult== TaskResult.OK)
{
MessageBox.Show("XXXX");
}
}

private void button6_Click(object sender, RoutedEventArgs e)
{
PhoneNumberChooserTask phoneNumberChooser = new PhoneNumberChooserTask() {

};
phoneNumberChooser.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooser_Completed);
phoneNumberChooser.Show();
}

void phoneNumberChooser_Completed(object sender, PhoneNumberResult e)
{
if (e.Error == null && e.TaskResult== TaskResult.OK)
{
MessageBox.Show(e.PhoneNumber );
}
}

private void button7_Click(object sender, RoutedEventArgs e)
{
CameraCaptureTask camerCapture = new CameraCaptureTask() {
};
camerCapture.Completed += new EventHandler<PhotoResult>(camerCapture_Completed);
camerCapture.Show();
}

void camerCapture_Completed(object sender, PhotoResult e)
{
if (e.Error == null && e.TaskResult == TaskResult.OK)
{
BitmapImage bmImage = new BitmapImage() {

};
bmImage.SetSource(e.ChosenPhoto);
image1.Source = bmImage;
}
}

private void button8_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask photoChooser = new PhotoChooserTask();
photoChooser.PixelHeight = 50;//截取的像素大小
photoChooser.PixelWidth = 50;
photoChooser.ShowCamera = true;
photoChooser.Completed += new EventHandler<PhotoResult>(photoChooser_Completed);
photoChooser.Show();
}

void photoChooser_Completed(object sender, PhotoResult e)
{
if (e.Error == null && e.TaskResult == TaskResult.OK)
{
BitmapImage bmImage = new BitmapImage();
bmImage.SetSource(e.ChosenPhoto);
image1.Source = bmImage;
}
}
}
}

程序在模拟器上运行结果如下:

      希望能对各位博友有帮助,祝各位博友开心进步~

      后话:在博文写到最后的时候,电脑忽然蓝屏了(一方面是开的东西太多,主要原因是显卡的驱动有问题,一直没有找到合适的驱动~),所以您现在看到的版本是Debug的第二版~吐槽一下,没有别的意思,希望对大家有用~

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1172933次
    • 积分:17100
    • 等级:
    • 排名:第603名
    • 原创:372篇
    • 转载:805篇
    • 译文:0篇
    • 评论:98条
    最新评论