<!--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="25,16,0,0" Name="textBlock1" Text="接收者:" VerticalAlignment="Top" FontSize="30" Width="144" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="12,62,0,0" Name="txtPhoneNumber" Text="" VerticalAlignment="Top" Width="444" >
<TextBox.InputScope>
<InputScope>
<InputScopeName NameValue="Number"/>
</InputScope>
</TextBox.InputScope>
</TextBox>
<TextBlock TextWrapping="Wrap" FontSize="30" Height="177" HorizontalAlignment="Left" Margin="6,270,0,0" Name="textBlock2" Text="短信内容" VerticalAlignment="Top" Width="46" />
<TextBox Height="406" HorizontalAlignment="Left" Margin="48,140,0,0" Name="txtMessage" Text="" VerticalAlignment="Top" Width="382" TextWrapping="Wrap" />
<Button Content="发送" Height="79" HorizontalAlignment="Left" Margin="-12,538,0,0" Name="btnSend" VerticalAlignment="Top" Width="480" Click="btnSend_Click" />
<Button Content="选择电话号码" Height="69" HorizontalAlignment="Left" Margin="210,0,0,0" Name="btnChoose" VerticalAlignment="Top" Width="220" Click="btnChoose_Click" />
</Grid>
</Grid>
后台代码:
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);
}
}