Windows Phone开发(47):轻松调用Web Service
原文:Windows Phone开发(47):轻松调用Web Service
众所周知(除了没用过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吧。
<phone:PhoneApplicationPage
x:Class="MyApp.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">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" VerticalAlignment="Center" Text="IP地址:"/>
<TextBox Name="txtIP" Grid.Column="1"/>
<Button Grid.Column="2" Click="onQuery">
<Button.Content>
<Path Data="M0,10 L20,10 M5,0 L20,10 M5,20 L20,10"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Stroke="White" StrokeThickness="3"/>
</Button.Content>
</Button>
</Grid>
<StackPanel Grid.Row="1">
<TextBlock Name="txbTip"/>
<TextBlock Name="txbResult" Margin="2,12,2,0" FontSize="32"/>
</StackPanel>
</Grid>
</Grid> </phone:PhoneApplicationPage>
3、打开“解决方案资源管理器”,右击“引用”节点,从弹出的菜单中选择“添加服务引用”。
4、在弹出的对话框中,“地址”处输入上文中提到的Web服务的地址,并点击“前往”按钮,待发现WEB服务完成后,在“命名空间”处输入一个有效命名空间名字,随你喜欢。最后别忘了单击“确定”。

5、切换到后台代码,完成查询按钮的单击事件处理。
private void onQuery(object sender, RoutedEventArgs e)
{
// 第一步,实例化客户端代理类
IPQueryWebService.IpAddressSearchWebServiceSoapClient MyClient = new IPQueryWebService.IpAddressSearchWebServiceSoapClient();
// 第二步,绑定回调事件
MyClient.getCountryCityByIpCompleted += (s, arg) =>
{
// 取得结果
txbTip.Text = "请求完成。";
if (arg.Error != null)
{
txtIP.Text = string.Format("错误:{0}", arg.Error.Message);
return;
}
string[] res = arg.Result;
if (res != null)
{
if (res.Length > 1)
{
txbResult.Text = string.Format("查询结果:{0}", res[1]);
}
}
};
// 第三步,调用异步方法
txbTip.Text = "正在请求,请等候……";
MyClient.getCountryCityByIpAsync(txtIP.Text);
}
好了,完成,记住WEB服务一般是异步调用,所以要在回调事件处理中取得调用结果。
运行一下,看看结果吧。

OK,就到这里吧。
Windows Phone开发(47):轻松调用Web Service的更多相关文章
- C#开发和调用Web Service
http://blog.csdn.net/h0322/article/details/4776819 1.1.Web Service基本概念 Web Service也叫XML Web Service ...
- [Teamcenter 2007 开发实战] 调用web service
前言 在TC的服务端开发中, 能够使用gsoap 来调用web service. 怎样使用 gsoap , 參考 gsoap 实现 C/C++ 调用web service 接下来介绍怎样在TC中进行 ...
- php5调用web service
工作中需要用php调用web service接口,对php不熟,上网搜搜,发现关于用php调用web service的文章也不多,不少还是php4里用nusoap这个模块调用的方法,其实php5里已经 ...
- ASP.NET调用Web Service
1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...
- php5调用web service (笔者测试成功)
转自:http://www.cnblogs.com/smallmuda/archive/2010/10/12/1848700.html 感谢作者分享 工作中需要用php调用web service接口, ...
- C# 调用 Web Service
Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是:通过SOAP ...
- ORACLE存储过程调用Web Service
1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sq ...
- 使用Android应用调用Web Service
Java本身提供了丰富的Web Service支持,比如Sun公司指定的JAX-WS 2规范,还有Apache开源组织所提供的Axis1.Axis2.CXF等,这些技术不仅可以用于非常方便地对外提 ...
- Dynamic CRM 2013学习笔记(二十五)JS调用web service 实现多条记录复制(克隆)功能
前面介绍过如何克隆一条当前的记录: Dynamic CRM 2013学习笔记(十四)复制/克隆记录 , 主要是通过界面上加一个字段,单击form上的clone 按钮时,改变这个字段的值以触发插件来实现 ...
随机推荐
- Swift - 项目部署配置(支持的系统,设备和状态条样式等)
点击项目,在General选项卡中的“Deployment Info”栏目中可以进行一些项目的配置 Deployment Target:支持的iOS SDK的最低版本 Device:所支持的设备(iP ...
- UVa11183 Teen Girl Squad, 最小树形图,朱刘算法
Teen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n teenage ...
- 四个机器学习一步一步入门约束波尔兹曼机RBM
- ConnectivityManager
ConnectivityManager 主要管理网络连接的相关的类它主要负责的是1 监视网络连接状态 包括(Wi-Fi, GPRS, UMTS, etc)2 当网络状态改变时发送广播通知3 当网络连接 ...
- J2EE之初识JSP
上篇博客已经简介了下Servlet.从上篇博客中能够看到.Servlet获得返回来的数据后.显示给client时,须要不断的拼串.从而构成完整的html页面,这就在无形中加大了程序猿的压力和劳动力.而 ...
- ios html5 设定PhoneGap开发环境
怎么样IOS平台搭建PhoneGap开发环境(PhoneGap2.5) (2013-03-13 14:44:51) 标签: c=blog&q=it&by=tag" targe ...
- 循环获取json对象的属性名
今天做项目遇到一个难题,asp.net 项目,数据库中一个表有八十多个字段,我已经在前台将表转化为了json字符数组,我要在前台循环这八十多个字段,我只能根据属性名来处理,一筹莫展,最终解决,收益颇多 ...
- 14.5.1 Resizing the InnoDB System Tablespace
14.5.1 Resizing the InnoDB System Tablespace 本节描述如何增加或者减少InnoDB 系统表空间的大小 增加InnoDB 系统表空间的大小 最简单的方式增加I ...
- 普联的路由器TL-WR842N和TL-WR845N还有 TL-WR847N哪一个更好,我是家用
http://zhidao.baidu.com/question/525244706.html?fr=ala&device=mobile&ssid=0&from=844b&am ...
- JavaScript学习总结1
/***我是切割线 的開始***/ //利用prototype属性能够加入公有属性和方法 function myConstructor2(){ this.a='大灰狼'; }; //声明构造函数,能够 ...