如何在Windows Phone 7上发送短信

来源:互联网 发布:空间数据库设计 编辑:程序博客网 时间:2024/05/14 06:31
 
这篇文章展示了如何在Windows Phone 7应用程序中发送短信。
简介
这篇文章描述了如何在Windows Phone 7应用程序中发送短信。短信的发送是利用SmsComposeTask API来完成的。要发送短信,请打开本地短信编辑器为用户提供一个选项,用来发送短信或放弃它。
概要
现在来讨论短信发送,让我们创建一个简单的windows phone应用程序,在the Visual Studio 2010中选择File->NewProject->Windows Phone Application。在应用程序的MainPage.xaml文件中添加一个文本Label(显示进入短信信息),TextBpx(输入短信内容)和一个Button(点击它,短信编辑器打开),如下所示:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
      <TextBox Height="92" HorizontalAlignment="Left" Margin="56,121,0,0" Name="textBox1"Text="" VerticalAlignment="Top" Width="289" />
      <TextBlock Height="52" HorizontalAlignment="Left" Margin="26,51,0,0" Name="textBlock1"Text="Please enter text for sms:" VerticalAlignment="Top" Width="256" />
      <Button Content="Send SMS" Height="86" HorizontalAlignment="Left" Margin="96,276,0,0"Name="button1" VerticalAlignment="Top" Width="198" Click="button1_Click" />   
</Grid>

这是在XAML上写的代码,创建的页面将如下所示: 
280px-SmsMain.jpg 
在MainPage.xaml.cs文件中,添加下面的代码:
using Microsoft.Phone.Tasks;
namespace SmsSender
  {
    .
    .
  
  // function which gets called when 'Send SMS' button is clicked
       string ismsboby;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
             ismsboby = textBox1.Text; // Fetch the text from textbox created in XAML
             SmsComposeTask smsComposeTask = new SmsComposeTask();
             smsComposeTask.To = "9012345566778"// Mention here the phone number to whom the sms is to be sent
             smsComposeTask.Body = ismsboby; // the string containing the sms body
             smsComposeTask.Show(); // this will invoke the native sms edtior
        }
  
  
    }
}

只要“Send SMS”按钮被点击,就会打开本地SMS应用程序,短信内容和电话号码将通过上面的代码进行填充。 
282px-Sendsms.jpg 
一旦用户在本地的SMS应用程序点击了“发送”按钮,短信将被发送。
287px-Sentsms_%282%29.jpg 

  
原创粉丝点击