Windows Phone 手机号码归属地Demo

  我最近半个月在学习Windows Phone的开发,在博客园看到一位园友写的WP7手机号码归属地的源码Demo,于是我按照这位前辈写的也学着写了一个。我在这里加以总结,方便日后复习,也希望得到其他前辈的指点,欢迎给位喜欢Windows Phone的友友们共同探讨。

  这个小应用的功能很简单,就是输入手机号码,查询号码归属地信息,主要用了一个WebService。然后我给它加上了打电话,和保存联系人的功能,也就是Task的调用。下图是我做的这个应用的截图,用的是微软的Emulator,比较丑陋,让各位见笑了。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 下面是创建程序的过程:

  1.创建Windows Phone应用程序。具体步骤:新建解决方案—>添加新项目—>在模板里面选择“Windows Phone应用程序"。如图:

 

  2.在MainPage.xaml页面做简单的布局。依次添加2个TextBlock控件,如第一幅图,一个Name为textBlockPhoneNo是用于提示输入的,还有一个Name 为txtBlockResult的是用于显示出现结果信息的;一个TextBox控件用于输入电话号码,三个按钮分别用于”查询“、”呼叫”和“保存"。布局的XAML 源码如下:

View Code
 1 <phone:PhoneApplicationPage 
 2     x:Class="PhoneNoTrack.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}" FontSize="24"/>
26             <TextBlock x:Name="PageTitle" Text="手机号码查询" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontSize="64" />
27         </StackPanel>
28 
29         <!--ContentPanel - 在此处放置其他内容-->
30         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
31             <TextBlock Height="48" HorizontalAlignment="Left" Margin="23,17,0,0" Name="textBlockPhoneNo" Text="请输入您要查询的手机号码" VerticalAlignment="Top" FontSize="32" />
32             <TextBox HorizontalAlignment="Left" Margin="19,71,0,451" Name="txtNo" Width="449" FontSize="36" />
33             <Button Content="查询" Height="92" HorizontalAlignment="Left" Margin="12,248,0,0" Name="btnSearch" VerticalAlignment="Top" Width="240" FontSize="36" Click="btnSearch_Click" />
34             <Button Content="拨打" Height="98" HorizontalAlignment="Right" Margin="0,350,204,0" Name="btnCall" VerticalAlignment="Top" Width="240" FontSize="36" Click="btnCall_Click" />
35             <Button Content="保存" Height="96" HorizontalAlignment="Left" Margin="12,446,0,0" Name="btnSave" VerticalAlignment="Top" Width="240" FontSize="36" Click="btnSave_Click" />
36             <TextBlock Height="68" HorizontalAlignment="Left" Margin="23,174,0,0" Name="txtBlockResult"  VerticalAlignment="Top" Width="415"  Foreground="#FFE5D409" FontSize="32" />
37         </Grid>
38     </Grid>
39 
40     <!--演示 ApplicationBar 用法的示例代码-->
41     <!--<phone:PhoneApplicationPage.ApplicationBar>
42         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
43             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按钮 1"/>
44             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按钮 2"/>
45             <shell:ApplicationBar.MenuItems>
46                 <shell:ApplicationBarMenuItem Text="菜单项 1"/>
47                 <shell:ApplicationBarMenuItem Text="菜单项 2"/>
48             </shell:ApplicationBar.MenuItems>
49         </shell:ApplicationBar>
50     </phone:PhoneApplicationPage.ApplicationBar>-->
51     
52 </phone:PhoneApplicationPage>

  3.添加WebService服务引用。引用地址为:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx

  4.在“查询”按钮的单击事件中创建引用MobileCodeWSSoapClient类的实例PhoneCodeWs,并且调用它的getMobileCodeInfoAsync(...)方法和getMobileCodeInfoCompleted事件,并且在getMobileCodeInfoCompleted事件完成之后将查询结果显示在txtBlockResult中。接着处理“拨打”和“保存"按钮的单击事件。废话不说了,代码如下:

View Code
 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 using Microsoft.Phone.Tasks;
14 
15 namespace PhoneNoTrack
16 {
17     public partial class MainPage : PhoneApplicationPage
18     {
19         // 构造函数
20         public MainPage()
21         {
22             InitializeComponent();
23         }
24 
25         private void btnSearch_Click(object sender, RoutedEventArgs e)
26         {
27             //实例化一个web service代理的对象
28             ServiceReference1.MobileCodeWSSoapClient PhoneCodeWs = new ServiceReference1.MobileCodeWSSoapClient();
29             PhoneCodeWs.getMobileCodeInfoCompleted += new EventHandler<ServiceReference1.getMobileCodeInfoCompletedEventArgs>(PhoneCodeWs_getMobileCodeInfoCompleted);
30             //将调用信息包括方法名和参数加入到soap消息中通过http传送给web service服务端  
31             PhoneCodeWs.getMobileCodeInfoAsync(txtNo.Text, "");
32         }
33 
34         void PhoneCodeWs_getMobileCodeInfoCompleted(object sender, ServiceReference1.getMobileCodeInfoCompletedEventArgs e)
35         {
36             if (e.Error != null)
37             {
38                 throw new Exception("网络异常或手机号码有误!");
39             }
40             else
41             {
42                 string msg = e.Result;
43                 try
44                 {
45                     this.txtBlockResult.Text = msg.Substring(12);
46                 }
47                 catch (Exception)
48                 {
49                     this.txtBlockResult.Text = "手机号码有误";
50                 }
51             }
52         }
53 
54         /// <summary>
55         /// 拨打电话
56         /// </summary>
57         /// <param name="sender"></param>
58         /// <param name="e"></param>
59         private void btnCall_Click(object sender, RoutedEventArgs e)
60         {
61             PhoneCallTask pcTask = new PhoneCallTask();
62             pcTask.PhoneNumber = this.txtNo.Text;
63             pcTask.Show();
64         }
65 
66         /// <summary>
67         /// 保存电话号码
68         /// </summary>
69         /// <param name="sender"></param>
70         /// <param name="e"></param>
71         private void btnSave_Click(object sender, RoutedEventArgs e)
72         {
73             SavePhoneNumberTask savePNTask = new SavePhoneNumberTask();
74             savePNTask.PhoneNumber = this.txtNo.Text;
75             savePNTask.Show();
76         }
77     }
78 }

  至此,一个简单的查询手机号码归属地的应用完成了,效果就如第一幅图。需要说明的一点,我调用的这个WebService功能不太完善,可能是没有更新吧,有的手机号码它查不到。比如我自己的手机号码就查不到,而我换成我朋友的和我以前的号码都能查到。

  参考资料:http://www.cnblogs.com/chihaodong/archive/2012/06/15/2550430.html

 

posted @ 2012-06-15 19:41  YunshiSun  阅读(1142)  评论(0编辑  收藏  举报