Windows Phone7啟動器與選取器

來源:互聯網
上載者:User

跟林永堅老師學習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啟動調試,如我們就跟蹤到了事件的調用順序等資訊:

 

 代碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.