天天看點

Windows Phone 7 使用啟動器(Lanucher)

Launcher啟動器:啟動Windows Phone 7内置應用程式。啟動器隻是負責把相應的應用程式啟動起來就可以了。

Lanucher 在使用時跟Chooser 相同,必須要注意應用程式生命周期;而Lanucher 主要的功能是呼叫Windows Phone 7 中其他的功能;例如說撥打電話、WebBrowser 等功能,使用Lanucher 時同樣的,必須要引入Microsoft .Phone.Tasks 的命名空間,下面筆者列出Lanucher 的各項功能

包括以下幾個:

EmailComposeTask: 啟動發送Email的應用程式。

MediaPlayerLauncher: 啟動MeidaPlayer應用程式。

PhoneCallTask: 啟動打電話應用程式。

SearchTask: 啟動搜尋應用程式。

SmsComposeTask: 啟動發短信應用程式。

WebBrowserTask: 啟動IE。

MarketplaceDetailTask: 啟動Marketplace用戶端應用程式,并顯示指定應用的詳細資訊。

MarketplaceHubTask: 啟動Marketplace用戶端應用程式。

MarketplaceReviewTask: 啟動Marketplace用戶端應用程式的審查頁面。

MarketplaceSearchTask: 啟動Marketplace用戶端應用程式的搜尋頁面。

1、EmailComposeTask

EmailComposeTask 讓您可以呼叫出系統預設的寄送Email 功能,并且在呼叫之前,能夠設定收件人,郵件内容等訊息,例如筆者做了以下的介面,執行的時候會出現像右圖這樣的錯誤訊息,這是因為内建的開發用模拟器沒有設定email 相關的帳号,是以無法做寄送email 的動作;不過不要緊,等有了實機之後就可以将應用程式部屬到機器上做實際的測試的

Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)
而程式碼的部分也是相當的簡單,例如下面這樣子

EmailComposeTask ect = new EmailComposeTask();

ect.To = txtEmailAddress.Text;

ect.Subject = txtSubject.Text;

ect.Body = txtMailBody.Text;

ect.Show();           
  1. View Code <!--LayoutRoot is the root grid where all page content is placed--> 
  2.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  3.         <Grid.RowDefinitions> 
  4.             <RowDefinition Height="Auto"/> 
  5.             <RowDefinition Height="*"/> 
  6.         </Grid.RowDefinitions> 
  7.         <!--TitlePanel contains the name of the application and page title--> 
  8.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  9.             <TextBlock x:Name="ApplicationTitle" Text="Lanucher Demo" Style="{StaticResource PhoneTextNormalStyle}"/> 
  10.             <TextBlock x:Name="PageTitle" Text="EmailCompose" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  11.         </StackPanel> 
  12.         <ScrollViewer Grid.Row="1"> 
  13.             <StackPanel x:Name="ContentPanel" > 
  14.                 <TextBlock Margin="3,1,1,1" Height="33" x:Name="textBlock1" Text="收件人郵件位址" /> 
  15.                 <TextBox x:Name="txtEmailAddress" Height="69" TextWrapping="Wrap" Text="TextBox" /> 
  16.                 <TextBlock Margin="3,1,1,1" Height="33" Name="textBlock2" Text="郵件主旨" /> 
  17.                 <TextBox Height="69" Name="txtSubject" Text="TextBox" TextWrapping="Wrap"/> 
  18.                 <TextBlock Margin="3,1,1,1" Height="33" Name="textBlock3" Text="郵件內容"  /> 
  19.                 <TextBox Height="274" Name="txtMailBody" Text="TextBox" TextWrapping="Wrap" /> 
  20.             </StackPanel> 
  21.         </ScrollViewer> 
  22.         <!--ContentPanel - place additional content here--> 
  23.     </Grid> 
  24.     <!--Sample code showing usage of ApplicationBar--> 
  25.     <phone:PhoneApplicationPage.ApplicationBar> 
  26.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 
  27.             <shell:ApplicationBarIconButton IconUri="/Images/appbar.check.rest.png" Text="OK" x:Name="btnOK" Click="btnOK_Click" /> 
  28.             <shell:ApplicationBarIconButton IconUri="/Images/appbar.cancel.rest.png" Text="Cancel" x:Name="btnCancel" /> 
  29.         </shell:ApplicationBar> 
  30.     </phone:PhoneApplicationPage.ApplicationBar> 
  1. View Code using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. using Microsoft.Phone.Tasks;  
  14. namespace LanucherDemo  
  15. {  
  16.     public partial class EmailCompose : PhoneApplicationPage  
  17.     {  
  18.         public EmailCompose()  
  19.         {  
  20.             InitializeComponent();  
  21.             txtEmailAddress.InputScope = new InputScope  
  22.             {  
  23.                 Names = {new InputScopeName() { NameValue = InputScopeNameValue.EmailNameOrAddress }}  
  24.             };  
  25.         }  
  26.         private void btnOK_Click(object sender, EventArgs e)  
  27.         {  
  28.             EmailComposeTask ect = new EmailComposeTask();  
  29.             ect.To = txtEmailAddress.Text;  
  30.             ect.Subject = txtSubject.Text;  
  31.             ect.Body = txtMailBody.Text;  
  32.             ect.Show();  
  33.         }  
  34.     }  

加菲貓

Just have a little faith.

Windows Phone 7 使用啟動器(Lanucher)

Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)
EmailComposeTask ect = new EmailComposeTask();

ect.To = txtEmailAddress.Text;

ect.Subject = txtSubject.Text;

ect.Body = txtMailBody.Text;

ect.Show();           
按 Ctrl+C 複制代碼View Code <!--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="Lanucher Demo" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="EmailCompose" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <ScrollViewer Grid.Row="1">
            <StackPanel x:Name="ContentPanel" >
                <TextBlock Margin="3,1,1,1" Height="33" x:Name="textBlock1" Text="收件人郵件位址" />
                <TextBox x:Name="txtEmailAddress" Height="69" TextWrapping="Wrap" Text="TextBox" />
                <TextBlock Margin="3,1,1,1" Height="33" Name="textBlock2" Text="郵件主旨" />
                <TextBox Height="69" Name="txtSubject" Text="TextBox" TextWrapping="Wrap"/>
                <TextBlock Margin="3,1,1,1" Height="33" Name="textBlock3" Text="郵件內容"  />
                <TextBox Height="274" Name="txtMailBody" Text="TextBox" TextWrapping="Wrap" />
            </StackPanel>
        </ScrollViewer>

        <!--ContentPanel - place additional content here-->
    </Grid>
 
    <!--Sample code showing usage of ApplicationBar-->
    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar.check.rest.png" Text="OK" x:Name="btnOK" Click="btnOK_Click" />
            <shell:ApplicationBarIconButton IconUri="/Images/appbar.cancel.rest.png" Text="Cancel" x:Name="btnCancel" />
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>按 Ctrl+C 複制代碼      
按 Ctrl+C 複制代碼View Code 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 LanucherDemo
{
    public partial class EmailCompose : PhoneApplicationPage
    {
        public EmailCompose()
        {
            InitializeComponent();
            txtEmailAddress.InputScope = new InputScope
            {
                Names = {new InputScopeName() { NameValue = InputScopeNameValue.EmailNameOrAddress }}
            };
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            EmailComposeTask ect = new EmailComposeTask();
            ect.To = txtEmailAddress.Text;
            ect.Subject = txtSubject.Text;
            ect.Body = txtMailBody.Text;
            ect.Show();
        }
    }
}按 Ctrl+C 複制代碼      

*註:在輸入 Email 的時候您也可以搭配 Chooser 來使用,會更加的友善

2、PhoneCallTask

PhoneCallTask 是能夠讓您在應用程式中去執行撥打電話的功能,先來看看執行效果

Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)

執行PhoneCallTask 時,需要先指定電話号碼以及顯示在畫面上的名稱(DisplayName),之後呼叫Show 的方法;呼叫Show 方法之後,首先會請使用者确認是否要撥打電話(中間的畫面),之後便會進行撥打電話的動作了;而程式碼的部分也相當的簡單,大緻會像下面這樣

PhoneCallTask pct = new PhoneCallTask();

pct.DisplayName = "Test Call";

pct.PhoneNumber = txtPhoneNo.Text;

pct.Show();           
  1. View Code <!--LayoutRoot is the root grid where all page content is placed--> 
  2.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  3.         <Grid.RowDefinitions> 
  4.             <RowDefinition Height="Auto"/> 
  5.             <RowDefinition Height="*"/> 
  6.         </Grid.RowDefinitions> 
  7.         <!--TitlePanel contains the name of the application and page title--> 
  8.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  9.             <TextBlock x:Name="ApplicationTitle" Text="Lanucher Demo" Style="{StaticResource PhoneTextNormalStyle}"/> 
  10.             <TextBlock x:Name="PageTitle" Text="Phone Call" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  11.         </StackPanel> 
  12.         <StackPanel x:Name="ContentPanel" Margin="12,0,12,0" Grid.Row="1" > 
  13.             <TextBlock Height="35" Margin="8,0" TextWrapping="Wrap" Text="電話號碼"/> 
  14.             <TextBox x:Name="txtPhoneNo" Height="72" Margin="8,0" TextWrapping="Wrap" Text=""/> 
  15.             <Button Content="Make call" Height="80" Margin="196,0,27,0" Click="Button_Click" /> 
  16.         </StackPanel> 
  17.         <!--ContentPanel - place additional content here--> 
  18.     </Grid> 
  1. View Code using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. using Microsoft.Phone.Tasks;  
  14. namespace LanucherDemo  
  15. {  
  16.     public partial class PhoneCall : PhoneApplicationPage  
  17.     {  
  18.         public PhoneCall()  
  19.         {  
  20.             InitializeComponent();  
  21.             txtPhoneNo.InputScope = new InputScope  
  22.             {  
  23.                 Names = { new InputScopeName { NameValue = InputScopeNameValue.TelephoneNumber } }  
  24.             };  
  25.         }  
  26.         private void Button_Click(object sender, RoutedEventArgs e)  
  27.         {  
  28.             PhoneCallTask pct = new PhoneCallTask();  
  29.             pct.DisplayName = "Test Call";  
  30.             pct.PhoneNumber = txtPhoneNo.Text;  
  31.             pct.Show();  
  32.         }  
  33.     }  

3、SerachTask

SearchTask 能夠讓應用程式指定查詢的關鍵字,并且去啟動系統預設的查詢功能;先來看看執行的效果

Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)

當您第一次去啟動SearchTask 時,您可能會看到中間的畫面,這是詢問使用者,是不是允許裝置利用GPS/AGPS 等方式去取得目前所在位置的一些相關資訊,确認之後就最出現最右邊的收尋結果畫面

SearchTask st = new SearchTask();

st.SearchQuery = txtKeyword.Text;

st.Show();           

4、SmscomposeTask

SmscomposeTask 讓應用程式可以呼叫系統的簡訊發送功能,先來看看執行的效果

Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)

使用的方式跟EmailComposeTask 相當的相似,隻要設定接收端的号碼以及SMS 内容之後,就可以啟動系統預設的簡訊發送介面了。程式碼的部分大緻會像下面這樣

SmsComposeTask sct = new SmsComposeTask(); sct.To = txtPhoneNo.Text; sct.Body = txtMessage.Text; sct.Show();

4、WebBrowserTask

WebBrowserTask 是啟動難建浏覽器的功能,并且前往您指定的位置,下面先來看一下執行之後的效果

Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)

像上圖的樣子,筆者是将WebBrowserTask 的URL 屬性設定為msdn.microsoft.com,在呼叫Show 的方法之後便會前往指定的頁面,程式碼大概會像下面這樣子

WebBrowserTask wbt = new WebBrowserTask();

wbt.URL = txtUrl.Text;

wbt.Show();           

5、 MediaPlayerLanucher

MediaPlayerLanucher 顧名思義就是去啟動MediaPlayer,而其中有幾個屬性要特别注意

  • Location

    Location 是描述檔案是放置在『什麼樣的位置』,在VS 環境中可以設定成下面三種

    MediaLocationType.Install:『Install』指的就是跟着你的xap 檔案一起部屬過去的相關檔案,也就是位于安裝的目錄中。

    MediaLocationType.Data:『Data』指的是位于隔離儲存區當中的檔案,也就是說如果您的檔案是執行之後才會取得或是産生的(例如說從網路下載下傳),而會将檔案寫入到隔離儲存區當中,這個時候就要設定為這個屬性。

    MediaLocationType.None:『None』的屬性目前來說是沒有作用的,如果設定為None,那麼呼叫Show 的方法之後,直接就會丢出『FileNotFroundException』。

  • Meida

    Media 是檔案的位置以及檔案名稱,是以Uri 的方式來表示,例如

    mpl.Media = new Uri(@"Media\test.wmv", UriKind.Relative);

  • Controls

    Controls 是設定MediaPlayer 出現之後,在畫面上會出現哪一些控制按鈕;而各個項目也可以利用OR 的方式去設定,例如說設定的方式可能會下面這樣

    //設定所有控制紐都出現

    mpl.Controls = MediaPlaybackControls.All;

    //設定出現停止按鈕以及暫停按鈕

    mpl.Controls = MediaPlaybackControls.Pause | MediaPlaybackControls.Stop;

接下來來看一下執行的效果,首先筆者建立了一個簡單的測試影片test.wmv,請特别注意,在Build Action 的部分要設定成Content,copy to output directory 要設定成『copy if newer』或是『always copy』

Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)

介面的部分相當簡單,隻有一個撥放的按鈕,點選之後就會進行撥放的動作(您如果在撥放的過程沒有看到畫面的話,請用滑鼠點選模拟器的畫面);接下來來看看程式碼的部分

MediaPlayerLauncher mpl = new MediaPlayerLauncher();

//指定檔案放置的位置屬性

mpl.Location = MediaLocationType.Install;

//mpl.Controls = MediaPlaybackControls.Pause | MediaPlaybackControls.Stop;

//指定播放的檔案

mpl.Show(); MarketPlaceDetailTask

6、MarketPlaceDetailTask

MarketPlaceDetailTask 主要的動作會啟動系統内建的MarketPlace 應用程式,并且可以指定要浏覽的應用程式ID;主要要特别注意兩個屬性;第一個是ContentType 的屬性,目前來說ContentType 一定要指定為Applications,如果指定為Music 的話,在呼叫Show 的方法時就會直接丢出錯誤了。而另一個是ContentIdentifier,這個屬性是要指定應用程式ID (

Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)

在短暫的搜尋動作之後換出現像是右圖的結果,這是因為目前在MarketPlace 上面找不到指定的應用程式的關系;相關動作的程式碼大緻會像是下面這樣

MarketplaceDetailTask mdt = new MarketplaceDetailTask();

//隻能設定為Applcations

mdt.ContentType = MarketplaceContentType.Applications;

//當沒有指定值則會以目前的應用程式為目标

//如果格式檢查不符合GUID 的格式則會丢出exception

mdt.ContentIdentifier = txtID.Text;

mdt.Show();

7、MarketplaceHubTask

MarketlaceHubTask 主要的功用是啟動後便會帶領使用者直接連線到Marketplace;操作的方法與其他Lanucher 類似,也是相當的簡單,要注意的是ContentType 屬性,可以設定為Application 與Music;下面就來看看執行的效果

Windows Phone 7 使用啟動器(Lanucher)
Windows Phone 7 使用啟動器(Lanucher)

中間畫面是當ContentType 設定為Music 時的畫面,最右邊則是設定為Application 時;您看到的畫面因為時間不同有可能跟筆者的是不相同的,各位可以自行實做看看;程式碼的部分也簡單的列出在下面

MarketplaceHubTask mht = new MarketplaceHubTask();

mht.ContentType = MarketplaceContentType.Applications;

//mht.ContentType = MarketplaceContentType.Music;

mht.Show();

8、 MarketplaceReviewTask

MarketplcaeReviewTask 的用途是在啟動之後會連到Marketplace 的頁面,并直接的為您的應用程式做評分、建議等地動作;使用方式相當的簡單,隻要利用下列的程式碼就可以了

MarketplaceReviewTask mrt = new MarketplaceReviewTask();

9、 MarketPlaceSearchTask

MarketplaceSearchTask 可以讓您搜尋Marketplace 上的應用程式或是音樂(一樣是透過ContentType 的屬性來設定),另外SearchTerms 可以指定關鍵字,例如說輸入sms 會得到類似下面的效果

Windows Phone 7 使用啟動器(Lanucher)
  1. View Code   
  2.  <!--LayoutRoot is the root grid where all page content is placed--> 
  3.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  4.         <Grid.RowDefinitions> 
  5.             <RowDefinition Height="Auto"/> 
  6.             <RowDefinition Height="*"/> 
  7.         </Grid.RowDefinitions> 
  8.         <!--TitlePanel contains the name of the application and page title--> 
  9.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  10.             <TextBlock x:Name="ApplicationTitle" Text="Lanucher Demo" Style="{StaticResource PhoneTextNormalStyle}"/> 
  11.             <TextBlock x:Name="PageTitle" Text="MarketPlace" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  12.         </StackPanel> 
  13.         <StackPanel x:Name="ContentPanel" Margin="12,0,12,0" Grid.Row="1" > 
  14.             <TextBlock Height="28" Margin="8,0" TextWrapping="Wrap" Text="應用程式 ID"/> 
  15.             <TextBox x:Name="txtID" Height="66" Margin="8,0" TextWrapping="Wrap" Text="cbd00900-0600-11db-89ca-0019b92a3933"/> 
  16.             <Button Content="Show Detail" Height="76" Margin="8,0,8,0" Click="Button_Click" Width="410" /> 
  17.             <Button Content="MarketplaceHub" Height="80" Name="button1" Width="409" Click="button1_Click" /> 
  18.             <Button Content="Application Review" Height="79" Name="button2" Width="409" Click="button2_Click" /> 
  19.             <TextBlock Height="28" Text="搜尋關鍵字" TextWrapping="Wrap" /> 
  20.             <TextBox Height="66" Name="txtSearchTerms" Text="sms" TextWrapping="Wrap" /> 
  21.             <Button Content="Search Marketplace" Height="79" Name="button3" Width="409" Click="button3_Click" /> 
  22.         </StackPanel> 
  23.         <!--ContentPanel - place additional content here--> 
  24.     </Grid> 
  1. View Code   
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Animation;  
  12. using System.Windows.Shapes;  
  13. using Microsoft.Phone.Controls;  
  14. using Microsoft.Phone.Tasks;  
  15. namespace LanucherDemo  
  16. {  
  17.     public partial class MarketPlaceDetail : PhoneApplicationPage  
  18.     {  
  19.         public MarketPlaceDetail()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.         private void Button_Click(object sender, RoutedEventArgs e)  
  24.         {  
  25.             MarketplaceDetailTask mdt = new MarketplaceDetailTask();  
  26.             //隻能設定為Applcation  
  27.             mdt.ContentType = MarketplaceContentType.Applications;  
  28.             //當沒有指定值則會以目前的應用程式為目標  
  29.             //如果格式檢查不符合GUID的格式則會丟出exception  
  30.             mdt.ContentIdentifier = txtID.Text;  
  31.             mdt.Show();     
  32.         }  
  33.         private void button1_Click(object sender, RoutedEventArgs e)  
  34.         {  
  35.             MarketplaceHubTask mht = new MarketplaceHubTask();  
  36.             mht.ContentType = MarketplaceContentType.Applications;  
  37.             //mht.ContentType = MarketplaceContentType.Music;  
  38.             mht.Show();  
  39.         }  
  40.         private void button2_Click(object sender, RoutedEventArgs e)  
  41.         {  
  42.             MarketplaceReviewTask mrt = new MarketplaceReviewTask();  
  43.             mrt.Show();  
  44.         }  
  45.         private void button3_Click(object sender, RoutedEventArgs e)  
  46.         {  
  47.             MarketplaceSearchTask mst = new MarketplaceSearchTask();  
  48.             //mst.ContentType = MarketplaceContentType.Music;  
  49.             mst.ContentType = MarketplaceContentType.Applications;  
  50.             mst.SearchTerms = txtSearchTerms.Text;  
  51.             mst.Show();  
  52.         }  
  53.     }  

繼續閱讀