`
mmdev
  • 浏览: 12964369 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

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的示例程序源代码。

    传智播客的android开发源代码

    29_发送xml数据和调用webservice.avi 所在项目:mobileAddressQuery & Web端应用:web 30_多线程下载原理.avi 所在项目:net 31_多线程断点下载器.avi 所在项目:MulThreadDownloader 32_文件断点上传器.avi 所在...

    Android、iPhone和Java、php四个平台一致的3des加密的类

    因为手机端后台通常是用php开发的Web Service,Android和iPhone客户端调用同样的Web Service接口,为了数据安全考虑,要对数据进行加密。以前用java做后台时候有过加密,就拿过来,用php调通了,在3个平台间加解密的...

    source.zip

    29_发送xml数据和调用webservice.avi 所在项目:mobileAddressQuery & Web端应用:web 30_多线程下载原理.avi 所在项目:net 31_多线程断点下载器.avi 所在项目:MulThreadDownloader 32_文件断点上传器.avi 所在...

    8天快速掌握Android教程源码

    29_发送xml数据和调用webservice.avi 所在项目:mobileAddressQuery & Web端应用:web 30_多线程下载原理.avi 所在项目:net 31_多线程断点下载器.avi 所在项目:MulThreadDownloader 32_文件断点上传器.avi 所在...

    一个新闻网站的iphone端工程代码

    一个综合网站的iPhone端XCode工程源码 ,使用SOAP调用Web Service,并解析XML结果,在iPhone上以Grid View,List View和Gallery方式显示网站内容,并有电子书下载和图片评注,活动提交等交互功能。

    《iOS6开发指南》精彩书摘

    最后介绍了程序外地图的使用,如何调用iOS 6苹果地图和调用谷歌Web地图。  第三部分进阶篇,介绍iOS高级内容,商业思考等,包括内容如下: 第14章“iOS中的商业模式”。了解iOS中的商业模式,其中的收费策略值得...

    Google Android SDK开发范例大全(第3版) 1/5

    Web Service存取服务:内嵌网页浏览器、Ajax网页特效、手机气象局、网络播放mp3、网络安装apk程序、远程下载手机铃声、XML-RPC移动博客发布器、手机RSS阅读器、地震速报、网页快照等。 完备的Google网络服务:Google...

    Google Android SDK开发范例大全(第3版) 4/5

    Web Service存取服务:内嵌网页浏览器、Ajax网页特效、手机气象局、网络播放mp3、网络安装apk程序、远程下载手机铃声、XML-RPC移动博客发布器、手机RSS阅读器、地震速报、网页快照等。 完备的Google网络服务:Google...

    Google Android SDK开发范例大全(第3版) 3/5

    Web Service存取服务:内嵌网页浏览器、Ajax网页特效、手机气象局、网络播放mp3、网络安装apk程序、远程下载手机铃声、XML-RPC移动博客发布器、手机RSS阅读器、地震速报、网页快照等。 完备的Google网络服务:Google...

    新版Android开发教程.rar

    � MVC 和 Web APP 架构 Android Android Android Android 开发背景 � 计算技术、无线接入技术的发展,使嵌入式系统逐渐有能力对桌面系统常规业务进行支持。 � 谷歌长期以来奉行的移动发展战略:通过与全球各地的...

    android源码包集合4

    从android中调用web service的源码.rar 从网络上获取图片.rar 仿iphone 气泡短信 DEMO.rar 仿ireader书架.zip 仿QQ微信登录页面.rar 仿UCWEB界面源码.rar 仿优酷Android客户端图片左右滑动(自动滑动).rar 仿...

    高清监控系统兼容所有网络摄像机

    Web Service=Web服务 Enable mobile server=启动手机服务 Service Port:=服务端口: Quality:=图像质量: FPS:=图像帧率: Date...=设置时间... Default Setting=默认配置 Enable talk server=允许语音对讲 high=高 ...

    一个适合新手学习的电商项目

    FastDFS (图片服务器),tomcat(web服务器),zookeeper(集群管理),mysql(数据库) Junit(测试) ### 技术栈: spring,springmvc,mybatis(框架) solr(搜索服务),redis(缓存),easyUI(后台系统页面) ### 数据库...

Global site tag (gtag.js) - Google Analytics