Windows Phone開發(fā)(47):輕松調(diào)用Web Service
眾所周知(除了沒用過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吧。
- <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 是包含所有頁面內(nèi)容的根網(wǎng)格-->
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <!--TitlePanel 包含應(yīng)用程序的名稱和頁標題-->
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="我的應(yīng)用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="頁面名稱" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
- <!--ContentPanel - 在此處放置其他內(nèi)容-->
- <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、打開“解決方案資源管理器”,右擊“引用”節(jié)點,從彈出的菜單中選擇“添加服務(wù)引用”。
4、在彈出的對話框中,“地址”處輸入上文中提到的Web服務(wù)的地址,并點擊“前往”按鈕,待發(fā)現(xiàn)WEB服務(wù)完成后,在“命名空間”處輸入一個有效命名空間名字,隨你喜歡。***別忘了單擊“確定”。
5、切換到后臺代碼,完成查詢按鈕的單擊事件處理。
- private void onQuery(object sender, RoutedEventArgs e)
- {
- // ***步,實例化客戶端代理類
- IPQueryWebService.IpAddressSearchWebServiceSoapClient MyClient = new IPQueryWebService.IpAddressSearchWebServiceSoapClient();
- // 第二步,綁定回調(diào)事件
- MyClient.getCountryCityByIpCompleted += (s, arg) =>
- {
- // 取得結(jié)果
- 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("查詢結(jié)果:{0}", res[1]);
- }
- }
- };
- // 第三步,調(diào)用異步方法
- txbTip.Text = "正在請求,請等候……";
- MyClient.getCountryCityByIpAsync(txtIP.Text);
- }
好了,完成,記住WEB服務(wù)一般是異步調(diào)用,所以要在回調(diào)事件處理中取得調(diào)用結(jié)果。
運行一下,看看結(jié)果吧。
OK,就到這里吧。