Windows Phone开发(24):启动器与选择器之发送短信

 

Windows Phone开发(24):启动器与选择器之发送短信

分类: 个人文章 1117人阅读 评论(1) 收藏 举报

本节我们通过一个简单的发送短信示例来演示一下如果配合使用PhoneNumberChooserTask和SmsComposeTask类。

PhoneNumberChooserTask是选择器,它用于从你的电话簿里选择你要发送短信的电话号码;

SmsComposeTask就是用来启动发送短信组件并显示发送窗口。

 

注意,这些操作都在用户的操控之中,发送短信一定会显示可视化页面的,而且不会偷偷地在后台发送,因为Windows phone是以用户体验和安全为原则的,后台发送是不允许的,而且发送过程是由用户控制的,你可以选择取消或退出。

 

SmsComposeTask类的To属性就是目标电话号码,Body就是你要发送的短信的正文。

 

同样,你会很轻松地就完成这个任务,不信,看看代码吧。

  1. <phone:PhoneApplicationPage   
  2.     x:Class="SMSSample.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"  
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  12.     Foreground="{StaticResource PhoneForegroundBrush}"  
  13.     SupportedOrientations="Portrait" Orientation="Portrait"  
  14.     shell:SystemTray.IsVisible="True">  
  15.   
  16.     <!--LayoutRoot 是包含所有页面内容的根网格-->  
  17.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  18.         <Grid.RowDefinitions>  
  19.             <RowDefinition Height="Auto"/>  
  20.             <RowDefinition Height="*"/>  
  21.         </Grid.RowDefinitions>  
  22.   
  23.         <!--TitlePanel 包含应用程序的名称和页标题-->  
  24.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  25.             <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>  
  26.             <TextBlock x:Name="PageTitle" Text="发送短信" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  27.         </StackPanel>  
  28.   
  29.         <!--ContentPanel - 在此处放置其他内容-->  
  30.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  31.             <TextBlock Height="40" HorizontalAlignment="Left" Margin="33,69,0,0" Name="textBlock1" Text="接收者:" VerticalAlignment="Top" FontSize="30" Width="144" />  
  32.             <TextBox Height="72" HorizontalAlignment="Left" Margin="12,116,0,0" Name="txtPhoneNumber" Text="" VerticalAlignment="Top" Width="418" >  
  33.                 <TextBox.InputScope>  
  34.                     <InputScope>  
  35.                         <InputScopeName NameValue="Number"/>  
  36.                     </InputScope>  
  37.                 </TextBox.InputScope>  
  38.             </TextBox>  
  39.             <TextBlock FontSize="30" Height="40" HorizontalAlignment="Left" Margin="33,235,0,0" Name="textBlock2" Text="短信内容:" VerticalAlignment="Top" Width="213" />  
  40.             <TextBox Height="233" HorizontalAlignment="Left" Margin="12,283,0,0" Name="txtMessage" Text="" VerticalAlignment="Top" Width="418" TextWrapping="Wrap" />  
  41.             <Button Content="发送" Height="79" HorizontalAlignment="Left" Margin="48,522,0,0" Name="btnSend" VerticalAlignment="Top" Width="357" Click="btnSend_Click" />  
  42.             <Button Content="选择电话号码" Height="72" HorizontalAlignment="Left" Margin="185,45,0,0" Name="btnChoose" VerticalAlignment="Top" Width="220" Click="btnChoose_Click" />  
  43.         </Grid>  
  44.     </Grid>  
  45.    
  46.   
  47. </phone:PhoneApplicationPage>  

 

  1. 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.   
  14. using Microsoft.Phone.Tasks;  
  15.   
  16. namespace SMSSample  
  17. {  
  18.     public partial class MainPage : PhoneApplicationPage  
  19.     {  
  20.         PhoneNumberChooserTask myChooser = new PhoneNumberChooserTask();  
  21.         SmsComposeTask SMS = null;  
  22.   
  23.         // 构造函数  
  24.         public MainPage()  
  25.         {  
  26.             InitializeComponent();  
  27.             // 实例化  
  28.             SMS = new SmsComposeTask();  
  29.             // 注册回调事件  
  30.             myChooser.Completed += (sender, e) => {  
  31.                 if (e.TaskResult== TaskResult.OK)  
  32.                 {  
  33.                     Dispatcher.BeginInvoke(() => { this.txtPhoneNumber.Text = e.PhoneNumber; });  
  34.                 }  
  35.             };  
  36.         }  
  37.   
  38.         // 选择联系人  
  39.         private void btnChoose_Click(object sender, RoutedEventArgs e)  
  40.         {  
  41.             if (myChooser == null)  
  42.             {  
  43.                 myChooser = new PhoneNumberChooserTask();  
  44.             }  
  45.             myChooser.Show();  
  46.         }  
  47.   
  48.         // 发送  
  49.         private void btnSend_Click(object sender, RoutedEventArgs e)  
  50.         {  
  51.             if (txtPhoneNumber.Text == "" || txtMessage.Text == "")  
  52.             {  
  53.                 MessageBox.Show("接收号码和短信内容不能为空。");  
  54.                 return;  
  55.             }  
  56.             if (SMS == null)  
  57.             {  
  58.                 SMS = new SmsComposeTask();  
  59.             }  
  60.             // 赋值  
  61.             SMS.To = txtPhoneNumber.Text;  
  62.             SMS.Body = txtMessage.Text;  
  63.   
  64.             try  
  65.             {  
  66.                 SMS.Show();  
  67.             }  
  68.             catch (Exception ex)  
  69.             {  
  70.                 MessageBox.Show(ex.Message);  
  71.             }  
  72.         }  
  73.     }  
  74. }  

 

 

 

 

 

posted @ 2012-11-01 09:04  BellingWP  阅读(164)  评论(0编辑  收藏  举报