自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Windows Phone開發(fā)(47):輕松調(diào)用Web Service

移動開發(fā)
移動設(shè)備現(xiàn)在緊密的跟互聯(lián)網(wǎng)結(jié)合,開發(fā)者應(yīng)該利用這個優(yōu)勢來開發(fā)各種網(wǎng)絡(luò)服務(wù),今天我們介紹在 Windows Phone 上開發(fā) Web 服務(wù)應(yīng)用。

眾所周知(除了沒用過VS的),在VS里面調(diào)用Web Service是一件很愉快的事情,不解釋,相信很多朋友在以前的項目中肯定也用過WEB服務(wù)。同樣,在WP中調(diào)用Web Service也是非常簡單的,你可以不信,反正我絕對信了。

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

為例,這個Web服務(wù)可以根據(jù)IP地址查詢相關(guān)的地區(qū)/城市信息,我們就通過調(diào)用這WEB服務(wù),來比較一下,在WP項目調(diào)用Web Service與我們以前做過的其它類型到底有沒有區(qū)別。

1、啟動VS,新建一個WP應(yīng)用程序項目(此處省略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 是包含所有頁面內(nèi)容的根網(wǎng)格-->   
  16.     <Grid x:Name="LayoutRoot" Background="Transparent">   
  17.         <Grid.RowDefinitions>   
  18.             <RowDefinition Height="Auto"/>   
  19.             <RowDefinition Height="*"/>   
  20.         </Grid.RowDefinitions>   
  21.         <!--TitlePanel 包含應(yīng)用程序的名稱和頁標題-->   
  22.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">   
  23.             <TextBlock x:Name="ApplicationTitle" Text="我的應(yīng)用程序" Style="{StaticResource PhoneTextNormalStyle}"/>   
  24.             <TextBlock x:Name="PageTitle" Text="頁面名稱" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>   
  25.         </StackPanel>   
  26.         <!--ContentPanel - 在此處放置其他內(nèi)容-->   
  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、打開“解決方案資源管理器”,右擊“引用”節(jié)點,從彈出的菜單中選擇“添加服務(wù)引用”。

4、在彈出的對話框中,“地址”處輸入上文中提到的Web服務(wù)的地址,并點擊“前往”按鈕,待發(fā)現(xiàn)WEB服務(wù)完成后,在“命名空間”處輸入一個有效命名空間名字,隨你喜歡。***別忘了單擊“確定”。

5、切換到后臺代碼,完成查詢按鈕的單擊事件處理。

  1. private void onQuery(object sender, RoutedEventArgs e)   
  2. {   
  3.     // ***步,實例化客戶端代理類   
  4.     IPQueryWebService.IpAddressSearchWebServiceSoapClient MyClient = new IPQueryWebService.IpAddressSearchWebServiceSoapClient();   
  5.     // 第二步,綁定回調(diào)事件   
  6.     MyClient.getCountryCityByIpCompleted += (s, arg) =>   
  7.         {   
  8.             // 取得結(jié)果   
  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("查詢結(jié)果:{0}", res[1]);   
  21.                 }   
  22.             }   
  23.         };   
  24.     // 第三步,調(diào)用異步方法   
  25.     txbTip.Text = "正在請求,請等候……";   
  26.     MyClient.getCountryCityByIpAsync(txtIP.Text);   

好了,完成,記住WEB服務(wù)一般是異步調(diào)用,所以要在回調(diào)事件處理中取得調(diào)用結(jié)果。

運行一下,看看結(jié)果吧。

OK,就到這里吧。

責任編輯:閆佳明 來源: Windows Phone
相關(guān)推薦

2013-07-30 12:37:56

Windows PhoWindows Pho

2010-04-21 17:07:54

Windows Pho

2013-04-16 17:02:50

Windows Pho概論

2013-04-19 16:34:56

Windows PhoWindows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2011-06-07 12:42:15

Windows Pho

2013-04-17 14:00:06

Windows PhoWindows Pho

2010-04-08 17:40:23

Windows Pho

2010-12-14 18:48:49

微軟

2013-07-31 13:13:50

Windows PhoMVVM模式

2012-06-04 14:47:58

Windows Pho

2013-04-19 15:35:54

Windows Pho隔離存儲

2013-04-19 16:52:24

Windows PhoWindows Pho

2013-07-31 12:50:39

搭建Windows PWindows Pho

2013-04-17 13:27:04

Windows PhoWindows Pho

2011-06-07 11:35:38

Windows Pho

2012-08-16 10:35:50

Windows Pho

2013-04-17 14:47:19

Windows PhoWindows Pho

2013-07-31 13:03:51

Windows PhoWindows Pho

2010-07-16 15:29:02

Windows Pho
點贊
收藏

51CTO技術(shù)棧公眾號