Windows Phone开发(47):轻松调用Web Service

移动开发
移动设备现在紧密的跟互联网结合,开发者应该利用这个优势来开发各种网络服务,今天我们介绍在 Windows Phone 上开发 Web 服务应用。

众所周知(除了没用过VS的),在VS里面调用Web Service是一件很愉快的事情,不解释,相信很多朋友在以前的项目中肯定也用过WEB服务。同样,在WP中调用Web Service也是非常简单的,你可以不信,反正我绝对信了。

有例子有真相,我们就以http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx

为例,这个Web服务可以根据IP地址查询相关的地区/城市信息,我们就通过调用这WEB服务,来比较一下,在WP项目调用Web Service与我们以前做过的其它类型到底有没有区别。

1、启动VS,新建一个WP应用程序项目(此处省略46个字)。

2、页面布局就参考下面XAML吧。

  1. <phone:PhoneApplicationPage    
  2.     x:Class="MyApp.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.     <!--LayoutRoot 是包含所有页面内容的根网格-->   
  16.     <Grid x:Name="LayoutRoot" Background="Transparent">   
  17.         <Grid.RowDefinitions>   
  18.             <RowDefinition Height="Auto"/>   
  19.             <RowDefinition Height="*"/>   
  20.         </Grid.RowDefinitions>   
  21.         <!--TitlePanel 包含应用程序的名称和页标题-->   
  22.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">   
  23.             <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>   
  24.             <TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>   
  25.         </StackPanel>   
  26.         <!--ContentPanel - 在此处放置其他内容-->   
  27.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">   
  28.             <Grid.RowDefinitions>   
  29.                 <RowDefinition Height="auto"/>   
  30.                 <RowDefinition Height="*"/>   
  31.             </Grid.RowDefinitions>   
  32.             <Grid Grid.Row="0">   
  33.                 <Grid.ColumnDefinitions>   
  34.                     <ColumnDefinition Width="auto"/>   
  35.                     <ColumnDefinition Width="*"/>   
  36.                     <ColumnDefinition Width="auto"/>   
  37.                 </Grid.ColumnDefinitions>   
  38.                 <TextBlock Grid.Column="0" VerticalAlignment="Center" Text="IP地址:"/>   
  39.                 <TextBox Name="txtIP" Grid.Column="1"/>   
  40.                 <Button Grid.Column="2" Click="onQuery">   
  41.                     <Button.Content>   
  42.                         <Path Data="M0,10 L20,10 M5,0 L20,10 M5,20 L20,10"   
  43.                               VerticalAlignment="Stretch"   
  44.                               HorizontalAlignment="Stretch"   
  45.                               Stroke="White" StrokeThickness="3"/>   
  46.                     </Button.Content>   
  47.                 </Button>   
  48.             </Grid>   
  49.             <StackPanel Grid.Row="1">   
  50.                 <TextBlock Name="txbTip"/>   
  51.                 <TextBlock Name="txbResult" Margin="2,12,2,0" FontSize="32"/>   
  52.             </StackPanel>   
  53.         </Grid>   
  54.     </Grid>   
  55. </phone:PhoneApplicationPage> 

3、打开“解决方案资源管理器”,右击“引用”节点,从弹出的菜单中选择“添加服务引用”。

4、在弹出的对话框中,“地址”处输入上文中提到的Web服务的地址,并点击“前往”按钮,待发现WEB服务完成后,在“命名空间”处输入一个有效命名空间名字,随你喜欢。***别忘了单击“确定”。

5、切换到后台代码,完成查询按钮的单击事件处理。

  1. private void onQuery(object sender, RoutedEventArgs e)   
  2. {   
  3.     // ***步,实例化客户端代理类   
  4.     IPQueryWebService.IpAddressSearchWebServiceSoapClient MyClient = new IPQueryWebService.IpAddressSearchWebServiceSoapClient();   
  5.     // 第二步,绑定回调事件   
  6.     MyClient.getCountryCityByIpCompleted += (s, arg) =>   
  7.         {   
  8.             // 取得结果   
  9.             txbTip.Text = "请求完成。";   
  10.             if (arg.Error != null)   
  11.             {   
  12.                 txtIP.Text = string.Format("错误:{0}", arg.Error.Message);   
  13.                 return;   
  14.             }   
  15.             string[] res = arg.Result;   
  16.             if (res != null)   
  17.             {   
  18.                 if (res.Length > 1)   
  19.                 {   
  20.                     txbResult.Text = string.Format("查询结果:{0}", res[1]);   
  21.                 }   
  22.             }   
  23.         };   
  24.     // 第三步,调用异步方法   
  25.     txbTip.Text = "正在请求,请等候……";   
  26.     MyClient.getCountryCityByIpAsync(txtIP.Text);   

好了,完成,记住WEB服务一般是异步调用,所以要在回调事件处理中取得调用结果。

运行一下,看看结果吧。

OK,就到这里吧。

责任编辑:闫佳明 来源: Windows Phone
相关推荐

2010-04-21 17:07:54

Windows Pho

2013-07-30 12:37:56

Windows PhoWindows Pho

2013-04-19 16:34:56

Windows PhoWindows Pho

2013-04-16 17:02:50

Windows Pho概论

2013-07-30 11:18:37

Windows PhoWindows Pho

2013-04-17 14:00:06

Windows PhoWindows Pho

2011-06-07 12:42:15

Windows Pho

2010-04-08 17:40:23

Windows Pho

2010-12-14 18:48:49

微软

2013-04-19 16:52:24

Windows PhoWindows Pho

2013-07-31 13:13:50

Windows PhoMVVM模式

2013-04-19 15:35:54

Windows Pho隔离存储

2012-06-04 14:47:58

Windows Pho

2013-07-31 12:50:39

搭建Windows PWindows Pho

2009-10-13 11:22:46

VB.NET调用Web

2013-04-17 14:47:19

Windows PhoWindows Pho

2012-08-16 10:35:50

Windows Pho

2013-04-17 13:27:04

Windows PhoWindows Pho

2011-06-07 11:35:38

Windows Pho

2010-07-16 15:29:02

Windows Pho
点赞
收藏

51CTO技术栈公众号