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

本节我们通过一个简单的发送短信示例来演示一下如果配合使用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>  
<phone:PhoneApplicationPage 
    x:Class="SMSSample.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 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <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 - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Height="40" HorizontalAlignment="Left" Margin="33,69,0,0" Name="textBlock1" Text="接收者:" VerticalAlignment="Top" FontSize="30" Width="144" />
            <TextBox Height="72" HorizontalAlignment="Left" Margin="12,116,0,0" Name="txtPhoneNumber" Text="" VerticalAlignment="Top" Width="418" >
                <TextBox.InputScope>
                    <InputScope>
                        <InputScopeName NameValue="Number"/>
                    </InputScope>
                </TextBox.InputScope>
            </TextBox>
            <TextBlock FontSize="30" Height="40" HorizontalAlignment="Left" Margin="33,235,0,0" Name="textBlock2" Text="短信内容:" VerticalAlignment="Top" Width="213" />
            <TextBox Height="233" HorizontalAlignment="Left" Margin="12,283,0,0" Name="txtMessage" Text="" VerticalAlignment="Top" Width="418" TextWrapping="Wrap" />
            <Button Content="发送" Height="79" HorizontalAlignment="Left" Margin="48,522,0,0" Name="btnSend" VerticalAlignment="Top" Width="357" Click="btnSend_Click" />
            <Button Content="选择电话号码" Height="72" HorizontalAlignment="Left" Margin="185,45,0,0" Name="btnChoose" VerticalAlignment="Top" Width="220" Click="btnChoose_Click" />
        </Grid>
    </Grid>
 

</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. }  
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 SMSSample
{
    public partial class MainPage : PhoneApplicationPage
    {
        PhoneNumberChooserTask myChooser = new PhoneNumberChooserTask();
        SmsComposeTask SMS = null;

        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            // 实例化
            SMS = new SmsComposeTask();
            // 注册回调事件
            myChooser.Completed += (sender, e) => {
                if (e.TaskResult== TaskResult.OK)
                {
                    Dispatcher.BeginInvoke(() => { this.txtPhoneNumber.Text = e.PhoneNumber; });
                }
            };
        }

        // 选择联系人
        private void btnChoose_Click(object sender, RoutedEventArgs e)
        {
            if (myChooser == null)
            {
                myChooser = new PhoneNumberChooserTask();
            }
            myChooser.Show();
        }

        // 发送
        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            if (txtPhoneNumber.Text == "" || txtMessage.Text == "")
            {
                MessageBox.Show("接收号码和短信内容不能为空。");
                return;
            }
            if (SMS == null)
            {
                SMS = new SmsComposeTask();
            }
            // 赋值
            SMS.To = txtPhoneNumber.Text;
            SMS.Body = txtMessage.Text;

            try
            {
                SMS.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值