本節我們通過一個簡單的傳送簡訊樣本來示範一下如果配合使用PhoneNumberChooserTask和SmsComposeTask類。
PhoneNumberChooserTask是選取器,它用於從你的電話簿裡選擇你要傳送簡訊的電話號碼;
SmsComposeTask就是用來啟動傳送簡訊組件並顯示發送視窗。
注意,這些操作都在使用者的操控之中,傳送簡訊一定會顯示可視化頁面的,而且不會偷偷地在後台發送,因為Windows phone是以使用者體驗和安全為原則的,後台發送是不允許的,而且發送過程是由使用者控制的,你可以選擇取消或退出。
SmsComposeTask類的To屬性就是目標電話號碼,Body就是你要發送的簡訊的本文。
同樣,你會很輕鬆地就完成這個任務,不信,看看代碼吧。
<phone:PhoneApplicationPage<br /> x:Class="SMSSample.MainPage"<br /> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br /> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br /> xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"<br /> xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"<br /> xmlns:d="http://schemas.microsoft.com/expression/blend/2008"<br /> xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"<br /> mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"<br /> FontFamily="{StaticResource PhoneFontFamilyNormal}"<br /> FontSize="{StaticResource PhoneFontSizeNormal}"<br /> Foreground="{StaticResource PhoneForegroundBrush}"<br /> SupportedOrientations="Portrait" Orientation="Portrait"<br /> shell:SystemTray.IsVisible="True"></p><p> <!--LayoutRoot 是包含所有頁面內容的根網格--><br /> <Grid x:Name="LayoutRoot" Background="Transparent"><br /> <Grid.RowDefinitions><br /> <RowDefinition Height="Auto"/><br /> <RowDefinition Height="*"/><br /> </Grid.RowDefinitions></p><p> <!--TitlePanel 包含應用程式的名稱和網頁標題--><br /> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"><br /> <TextBlock x:Name="ApplicationTitle" Text="我的應用程式" Style="{StaticResource PhoneTextNormalStyle}"/><br /> <TextBlock x:Name="PageTitle" Text="傳送簡訊" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/><br /> </StackPanel></p><p> <!--ContentPanel - 在此處放置其他內容--><br /> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"><br /> <TextBlock Height="40" HorizontalAlignment="Left" Margin="33,69,0,0" Name="textBlock1" Text="接收者:" VerticalAlignment="Top" FontSize="30" Width="144" /><br /> <TextBox Height="72" HorizontalAlignment="Left" Margin="12,116,0,0" Name="txtPhoneNumber" Text="" VerticalAlignment="Top" Width="418" ><br /> <TextBox.InputScope><br /> <InputScope><br /> <InputScopeName NameValue="Number"/><br /> </InputScope><br /> </TextBox.InputScope><br /> </TextBox><br /> <TextBlock FontSize="30" Height="40" HorizontalAlignment="Left" Margin="33,235,0,0" Name="textBlock2" Text="簡訊內容:" VerticalAlignment="Top" Width="213" /><br /> <TextBox Height="233" HorizontalAlignment="Left" Margin="12,283,0,0" Name="txtMessage" Text="" VerticalAlignment="Top" Width="418" TextWrapping="Wrap" /><br /> <Button Content="發送" Height="79" HorizontalAlignment="Left" Margin="48,522,0,0" Name="btnSend" VerticalAlignment="Top" Width="357" Click="btnSend_Click" /><br /> <Button Content="選擇電話號碼" Height="72" HorizontalAlignment="Left" Margin="185,45,0,0" Name="btnChoose" VerticalAlignment="Top" Width="220" Click="btnChoose_Click" /><br /> </Grid><br /> </Grid></p><p></phone:PhoneApplicationPage>
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Net;<br />using System.Windows;<br />using System.Windows.Controls;<br />using System.Windows.Documents;<br />using System.Windows.Input;<br />using System.Windows.Media;<br />using System.Windows.Media.Animation;<br />using System.Windows.Shapes;<br />using Microsoft.Phone.Controls;</p><p>using Microsoft.Phone.Tasks;</p><p>namespace SMSSample<br />{<br /> public partial class MainPage : PhoneApplicationPage<br /> {<br /> PhoneNumberChooserTask myChooser = new PhoneNumberChooserTask();<br /> SmsComposeTask SMS = null;</p><p> // 建構函式<br /> public MainPage()<br /> {<br /> InitializeComponent();<br /> // 執行個體化<br /> SMS = new SmsComposeTask();<br /> // 註冊回調事件<br /> myChooser.Completed += (sender, e) => {<br /> if (e.TaskResult== TaskResult.OK)<br /> {<br /> Dispatcher.BeginInvoke(() => { this.txtPhoneNumber.Text = e.PhoneNumber; });<br /> }<br /> };<br /> }</p><p> // 選擇連絡人<br /> private void btnChoose_Click(object sender, RoutedEventArgs e)<br /> {<br /> if (myChooser == null)<br /> {<br /> myChooser = new PhoneNumberChooserTask();<br /> }<br /> myChooser.Show();<br /> }</p><p> // 發送<br /> private void btnSend_Click(object sender, RoutedEventArgs e)<br /> {<br /> if (txtPhoneNumber.Text == "" || txtMessage.Text == "")<br /> {<br /> MessageBox.Show("接收號碼和簡訊內容不可為空。");<br /> return;<br /> }<br /> if (SMS == null)<br /> {<br /> SMS = new SmsComposeTask();<br /> }<br /> // 賦值<br /> SMS.To = txtPhoneNumber.Text;<br /> SMS.Body = txtMessage.Text;</p><p> try<br /> {<br /> SMS.Show();<br /> }<br /> catch (Exception ex)<br /> {<br /> MessageBox.Show(ex.Message);<br /> }<br /> }<br /> }<br />}