Windows Phone7启动器与选择器

junwong 发布于 2012/03/05 16:55
阅读 462
收藏 0

【华为云1024程序员节·向云而生】预约直播 抽14件华为电子产品礼包!>>>

跟林永坚老师学习wp7

 

  • WIndowsPhone执行模型决定了 每个应用程序只能在自己的沙盒里运行
  • 应用程序不可以直接访问其他存储信息(例如联系的信息),
  • 应用程序不可以直接调用其他功能(如电话或短信功能)
  • 启动器和选择器为应用程序提供了间接访问这些功能的方法
  • 启动器和选择器运行时操作系统可能会终止当前的应用程序

 

 

  1.  启动器:启动一个内置的程序给用户使用,不会返回任何的数据给调用的程序,例如搜索任务
  2. 选择器:启动一个内置的程序给用户使用, 返回一些数据给调用的程序,当用户完成操作时,程序会被重新激活,并接受到返回的数据,如果用户取消操作,返回NULL,例如图片选择器

    wp7内置的启动器

  • EmailComposeTask –允许用户通过邮件账户发送电子邮件。
  • MarketplaceDetailTask –启动Windows Phone Marketplace,让用户查看某个特定的产品。
  • MarketplaceHubTask –启动Windows Phone Marketplace,默认情况下允许你显示某一类别的应用程序。
  • MarketplaceReviewTask –将用户带到Windows Phone Marketplace中当前程序的评论页。
  • MarketplaceSearchTask –启动Windows Phone Marketplace的搜索结果,此结果来源于用户输入的搜索项(或者是你来制定的)。
  • MediaPlayerLauncher –启动内置的媒体播放器,并播放你指定的媒体文件。
  • PhoneCallTask –启动电话程序并显示电话号码和姓名。电话只在用户点击“通话”后才会拨出。
  • SearchTask – 可以把这个看成是在你的程序中提供Bing搜索功能的途径。
  • SMSComposeTask –启动信息程序并向用户展示发送信息的功能。你可以指定信息接收人和信息内容,但只能通过用户来发送。
  • WebBrowserTask –打开网络浏览器,跳转到指定的URL。

wp7内置的选择器

启动器的使用时的注意事项:

 声明一个Chooser:

       PhotoChooserTask photoChooserTask;

NeW一个Chooser的实例:并注册CompleteD事件

      public MainPage()
        {
            InitializeComponent();
            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
}

 实现Completed事件:

      void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                MessageBox.Show(e.ChosenPhoto.Length.ToString());

                
                System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
                bmp.SetSource(e.ChosenPhoto);
                myImage.Source = bmp;
            }

        }

 总结:启动器和选择器的最明显区别就是启动器没有返回任何信息,所以不需要注册Completed的事件,而选择器则会返回一些信息,所以需要注册Completed的事件,他们都需要先NEw一个实例,然后配置参数,最后SHOw()就可以实现调用;

 后台完整代码:

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 LaunchersAndChoosers
{
    public partial class MainPage : PhoneApplicationPage
    {
        PhotoChooserTask photoChooserTask;
        SavePhoneNumberTask savePhoneNumberTask;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
            savePhoneNumberTask = new SavePhoneNumberTask();
            savePhoneNumberTask.Completed += new EventHandler<TaskEventArgs>(savePhoneNumberTask_Completed);
        }

        void savePhoneNumberTask_Completed(object sender, TaskEventArgs e)
        {
            switch (e.TaskResult)
            {
                //Logic for when the number was saved successfully
                case TaskResult.OK:
                    MessageBox.Show("Phone number saved.");
                    break;

                //Logic for when the task was cancelled by the user
                case TaskResult.Cancel:
                    MessageBox.Show("Save cancelled.");
                    break;

                //Logic for when the number could not be saved
                case TaskResult.None:
                    MessageBox.Show("Phone number could not be saved.");
                    break;
            }

        }

        void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                MessageBox.Show(e.ChosenPhoto.Length.ToString());

                
                System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
                bmp.SetSource(e.ChosenPhoto);
                myImage.Source = bmp;
            }

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SearchTask tastk = new SearchTask();
            tastk.SearchQuery = "littlefeihu";
            tastk.Show();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            PhoneCallTask task = new PhoneCallTask();
            task.PhoneNumber = "123546879";
            task.DisplayName = "littlefeihu";
            task.Show();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            photoChooserTask.Show();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                savePhoneNumberTask.PhoneNumber = "2065550123";

                savePhoneNumberTask.Show();
            }
            catch (System.InvalidOperationException ex)
            {
                MessageBox.Show("An error occurred.");
            }

        }
    }
}

前段Xaml代码:

<phone:PhoneApplicationPage 
    x:Class="LaunchersAndChoosers.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="演示程序" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="启动器与选择器" 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="40,93,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
            <Button Content="打电话" Height="72" HorizontalAlignment="Left" Margin="247,93,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
            <Button Content="选择图片" Height="72" HorizontalAlignment="Left" Margin="40,251,0,0" Name="button3" VerticalAlignment="Top" Width="160" Click="button3_Click" />
            <Button Content="保存电话" Height="72" HorizontalAlignment="Left" Margin="247,251,0,0" Name="button4" VerticalAlignment="Top" Width="160" Click="button4_Click" />
            <Image Height="196" HorizontalAlignment="Left" Margin="12,354,0,0" Name="myImage" Stretch="Fill" VerticalAlignment="Top" Width="407" />
        </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>

 对于应用程序事件的跟踪,我们可以通过VS自带的OutPut窗口来观察,方便好用

比如我们在后台写上如下的跟踪代码:

打开output窗口,F5启动调试,如下图我们就跟踪到了事件的调用顺序等信息:

 

 代码下载


原文链接:http://www.cnblogs.com/LittleFeiHu/archive/2012/03/04/2377687.html
加载中
返回顶部
顶部