Windows Phone 7 SDK学习之Microsoft.Phone.Tasks中的各项Task

  有许多人或许会问,Windows Phone 7不允许访问本机的功能,使我们自己的程序完成共同的任务,比如发短信、发送email,但是你或许不能直接访问,但是Windows Phone 7 提供了间接的访问方式,那就是 Launchers & Choosers。通过他们我们就可以访问Windows Phone 7系统手机中的各项本机功能,并且微软已经为我们使用这些功能封装好了,我们只需要一个简单的调用就完全实现了。下面我们预览一下效果。

                    

 

            

      

 

  接下来我们进入正题

  1.PhoneCallTask :打电话

  private void btnPhoneCall_Click(object sender, RoutedEventArgs e)
        {
            PhoneCallTask task = new PhoneCallTask();


            task.PhoneNumber = "12345678901";


            task.DisplayName = "吴建强";


            task.Show();
        }

 

  2.SmsComposeTask:发送短信 

 private void btnComposeSms_Click(object sender, RoutedEventArgs e)
        {
            SmsComposeTask task = new SmsComposeTask();


            task.To = "12345678901";


            task.Body = "你好!.";


            task.Show();
        }

 

  3.EmailComposeTask:发送邮件

 

EmailComposeTask task = new EmailComposeTask();


            task.To = "wjqlxt@163.com";


            task.Cc = "875867090@qq.com";


            task.Subject = "测试";


            task.Body = "你好啊!";


            task.Show();

 

 

 

  4.WebBrowserTask:开启Web浏览器

 

  private void btnLaunchWebBrowser_Click(object sender, RoutedEventArgs e)
        {
            WebBrowserTask task = new WebBrowserTask();


            task.URL = "http://www.cnblogs.com/salam";
            task.Show();
        }

 

  5.SearchTask:搜索功能

 

private void btnLaunchSearch_Click(object sender, RoutedEventArgs e)
        {
            SearchTask task = new SearchTask();


            task.SearchQuery = "吴建强";
            task.Show();
        }

 

  6.MediaPlayerLauncher:启动媒体播放器

  MediaPlayerLauncher task = new MediaPlayerLauncher();


   task.Media = new Uri(@"http://ecn.channel9.msdn.com/o9/learn/videos/Silverlight4-SL4BusinessModule1-SL4LOB_01_01_Introduction/Silverlight4-SL4BusinessModule1-SL4LOB_01_01_Introduction_kit.wmv");


            task.Show();

 

 

 

  7.CameraCaptureTask:照相功能

 private void btnTakePhoto_Click(object sender, RoutedEventArgs e)
        {
            CameraCaptureTask task = new CameraCaptureTask();


            task.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    BitmapImage bmpImage = new BitmapImage();
                    bmpImage.SetSource(evt.ChosenPhoto);
                    image.Source = bmpImage;
                }
            };
            task.Show();
        }

 

  8.PhotoChooserTask:选择照片功能

 private void btnSelectPhoto_Click(object sender, RoutedEventArgs e)
        {
            PhotoChooserTask task = new PhotoChooserTask();
            task.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    BitmapImage bmpImage = new BitmapImage();
                    bmpImage.SetSource(evt.ChosenPhoto);
                    image.Source = bmpImage;
                }
            };
            task.Show();
        }

 

  9.PhoneNumberChooserTask:选择联系人

 

 private void btnSelectPhoneNumber_Click(object sender, RoutedEventArgs e)
        {
            PhoneNumberChooserTask task = new PhoneNumberChooserTask();


            task.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    MessageBox.Show(evt.PhoneNumber + "请选择电话号码");
                }
            };


            task.Show();


        }

 

  10.EmailAddressChooserTask:选择邮箱

 private void btnSelectEmailAddress_Click(object sender, RoutedEventArgs e)
        {
            EmailAddressChooserTask task = new EmailAddressChooserTask();


            task.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    MessageBox.Show(evt.Email + " 请选择邮件地址");
                }
            };


            task.Show();


        }

 

  11.SavePhoneNumberTask:保存电话号码

 private void btnSavePhoneNumber_Click(object sender, RoutedEventArgs e)
        {
            SavePhoneNumberTask task = new SavePhoneNumberTask();


            task.PhoneNumber = "12345678901";


            task.Show();
        }

 

  12.SaveEmailAddressTask:保存邮箱

 

private void btnSaveEmailAddress_Click(object sender, RoutedEventArgs e)
        {
            SaveEmailAddressTask task = new SaveEmailAddressTask();


            task.Email = "875867090@qq.com";


            task.Show();
        }

 

  13.PhoneNumberChooserTask和SmsComposeTask的结合使用

private void btnSms_Click(object sender, RoutedEventArgs e)
        {
            PhoneNumberChooserTask contactsTask = new PhoneNumberChooserTask();


            contactsTask.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    SmsComposeTask smsTask = new SmsComposeTask();
                    smsTask.Body = "编辑内容.";
                    smsTask.To = evt.PhoneNumber;
                    smsTask.Show();
                }
            };


            contactsTask.Show();
        }

 

  14.EmailAddressChooserTask和EmailComposeTask的结合使用

        private void btnEmail_Click(object sender, RoutedEventArgs e)
        {
            EmailAddressChooserTask contactsTask = new EmailAddressChooserTask();


            contactsTask.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    EmailComposeTask emailTask = new EmailComposeTask();
                    emailTask.Body = "编辑内容";
                    emailTask.To = evt.Email;
                    emailTask.Show();
                }
            };


            contactsTask.Show();


        }

 

 

  完整MainPage.xaml

<phone:PhoneApplicationPage
    x:Class="WindowsPhoneTasks.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="WINDOWS PHONE 7 LAUNCHERS &amp; CHOOSERS" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Tasks" 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">
            <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto">
                <StackPanel Orientation="Vertical">
                    <TextBlock>Launchers</TextBlock>
                    <Button Name="btnPhoneCall" FontSize="30" Content="打电话" Click="btnPhoneCall_Click" />
                    <Button Name="btnComposeSms" FontSize="30" Content="发送短信" Click="btnComposeSms_Click" />
                    <Button Name="btnComposeEmail" FontSize="30"  Content="发送邮件" Click="btnComposeEmail_Click" />
                    <Button Name="btnLaunchWebBrowser" FontSize="30" Content="启动浏览器" Click="btnLaunchWebBrowser_Click" />
                    <Button Name="btnLaunchSearch" FontSize="30" Content="启动搜索功能" Click="btnLaunchSearch_Click" />
                    <Button Name="btnLaunchMediaPlayer" FontSize="30" Content="启动媒体播放器" Click="btnLaunchMediaPlayer_Click" />
                    <TextBlock>Choosers</TextBlock>
                    <Button Name="btnTakePhoto" FontSize="30" Content="照相" Click="btnTakePhoto_Click" />
                    <Image Name="image" />
                    <Button Name="btnSelectPhoto" FontSize="30" Content="选择照片" Click="btnSelectPhoto_Click" />
                    <Button Name="btnSelectPhoneNumber" FontSize="30" Content="选择电话号码" Click="btnSelectPhoneNumber_Click" />
                    <Button Name="btnSelectEmailAddress" FontSize="30" Content="选择邮件地址" Click="btnSelectEmailAddress_Click" />
                    <Button Name="btnSavePhoneNumber" FontSize="30" Content="保存电话号码" Click="btnSavePhoneNumber_Click" />
                    <Button Name="btnSaveEmailAddress" FontSize="30" Content="保存邮件地址" Click="btnSaveEmailAddress_Click" />
                    <TextBlock FontSize="30"> Launchers和Choosers结合</TextBlock>
                    <Button Name="btnSms" FontSize="30"  Content="编辑短信" Click="btnSms_Click" />
                    <Button Name="btnEmail" FontSize="30"  Content="编辑邮件" Click="btnEmail_Click" />
                </StackPanel>
            </ScrollViewer>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

 

  完整MainPage.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.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace WindowsPhoneTasks
{
    public partial class MainPage : PhoneApplicationPage
    {
        #region Constructor

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        #endregion

        #region Launchers

        private void btnPhoneCall_Click(object sender, RoutedEventArgs e)
        {
            PhoneCallTask task = new PhoneCallTask();
            task.PhoneNumber = "12345678901";
            task.DisplayName = "吴建强";
            task.Show();
        }

        private void btnComposeSms_Click(object sender, RoutedEventArgs e)
        {
            SmsComposeTask task = new SmsComposeTask();
            task.To = "12345678901";
            task.Body = "你好!.";
            task.Show();
        }

        private void btnComposeEmail_Click(object sender, RoutedEventArgs e)
        {
            EmailComposeTask task = new EmailComposeTask();
            task.To = "wjqlxt@163.com";
            task.Cc = "875867090@qq.com";
            task.Subject = "测试";
            task.Body = "你好啊!";
            task.Show();
        }

        private void btnLaunchWebBrowser_Click(object sender, RoutedEventArgs e)
        {
            WebBrowserTask task = new WebBrowserTask();
            task.URL = "http://www.cnblogs.com/salam";
            task.Show();
        }

        private void btnLaunchSearch_Click(object sender, RoutedEventArgs e)
        {
            SearchTask task = new SearchTask();
            task.SearchQuery = "吴建强";
            task.Show();
        }

        private void btnLaunchMediaPlayer_Click(object sender, RoutedEventArgs e)
        {
            MediaPlayerLauncher task = new MediaPlayerLauncher();
            task.Media = new Uri(@"http://ecn.channel9.msdn.com/o9/learn/videos/Silverlight4-SL4BusinessModule1-SL4LOB_01_01_Introduction/Silverlight4-SL4BusinessModule1-SL4LOB_01_01_Introduction_kit.wmv");
            task.Show();
        }

        #endregion

        #region Choosers

        private void btnTakePhoto_Click(object sender, RoutedEventArgs e)
        {
            CameraCaptureTask task = new CameraCaptureTask();
            task.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    BitmapImage bmpImage = new BitmapImage();
                    bmpImage.SetSource(evt.ChosenPhoto);
                    image.Source = bmpImage;
                }
            };
            task.Show();
        }

        private void btnSelectPhoto_Click(object sender, RoutedEventArgs e)
        {
            PhotoChooserTask task = new PhotoChooserTask();
            task.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    BitmapImage bmpImage = new BitmapImage();
                    bmpImage.SetSource(evt.ChosenPhoto);
                    image.Source = bmpImage;
                }
            };
            task.Show();
        }

        private void btnSelectPhoneNumber_Click(object sender, RoutedEventArgs e)
        {
            PhoneNumberChooserTask task = new PhoneNumberChooserTask();
            task.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    MessageBox.Show(evt.PhoneNumber + "请选择电话号码");
                }
            };
            task.Show();
        }

        private void btnSelectEmailAddress_Click(object sender, RoutedEventArgs e)
        {
            EmailAddressChooserTask task = new EmailAddressChooserTask();
            task.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    MessageBox.Show(evt.Email + " 请选择邮件地址");
                }
            };
            task.Show();
        }

        private void btnSavePhoneNumber_Click(object sender, RoutedEventArgs e)
        {
            SavePhoneNumberTask task = new SavePhoneNumberTask();
            task.PhoneNumber = "12345678901";
            task.Show();
        }

        private void btnSaveEmailAddress_Click(object sender, RoutedEventArgs e)
        {
            SaveEmailAddressTask task = new SaveEmailAddressTask();
            task.Email = "875867090@qq.com";
            task.Show();
        }

        #endregion

        #region  Launchers 和 Choosers的结合

        private void btnSms_Click(object sender, RoutedEventArgs e)
        {
            PhoneNumberChooserTask contactsTask = new PhoneNumberChooserTask();
            contactsTask.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    SmsComposeTask smsTask = new SmsComposeTask();
                    smsTask.Body = "编辑内容.";
                    smsTask.To = evt.PhoneNumber;
                    smsTask.Show();
                }
            };
            contactsTask.Show();
        }

        private void btnEmail_Click(object sender, RoutedEventArgs e)
        {
            EmailAddressChooserTask contactsTask = new EmailAddressChooserTask();
            contactsTask.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    EmailComposeTask emailTask = new EmailComposeTask();
                    emailTask.Body = "编辑内容";
                    emailTask.To = evt.Email;
                    emailTask.Show();
                }
            };
            contactsTask.Show();
        }

        #endregion
    }
}

posted @ 2011-01-03 23:03  ForrestWoo  阅读(1534)  评论(2编辑  收藏  举报