As we all know (except those who have never used vs), it is a pleasure to call Web Service in, I believe many of my friends have used web services in their previous projects. Similarly, it is very easy to call Web Service in WP. You can believe it or not. I believe it.
If there is an example of the truth, we use http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx
For example, this web service can query the relevant region/city information based on the IP address. We can call this web service to compare it, is there any difference between calling Web Service in the WP project and other types we have previously done.
1. Start Vs and create a WP application.ProgramProject (46 words are omitted here ).
2. For the page layout, refer to the following 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 is the root grid containing all page content->
<Grid x: Name = "LayoutRoot" Background = "Transparent">
<Grid.RowDefinitions>
<RowDefinition Height = "Auto" />
<RowDefinition Height = "*" />
</Grid.RowDefinitions>
<!-TitlePanel contains the application name and page title->
<StackPanel x: Name = "TitlePanel" Grid.Row = "0" Margin = "12,17,0,28">
<TextBlock x: Name = "ApplicationTitle" Text = "My Application" Style = "{StaticResource PhoneTextNormalStyle}" />
<TextBlock x: Name = "PageTitle" Text = "Page Name" Margin = "9, -7,0,0" Style = "{StaticResource PhoneTextTitle1Style}" />
</ StackPanel>
<!-ContentPanel-place additional content here->
<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 address:" />
<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. Open Solution Explorer, right-click the reference node, and select add service reference from the pop-up menu ".
4. In the pop-up dialog box, enter the address of the web service mentioned above and click "go". After the Web Service is found, enter a valid namespace name in the namespace field. Finally, don't forget to click "OK ".
5. Switch to the backgroundCodeTo complete event processing.
private void onQuery (object sender, RoutedEventArgs e)
{
// The first step is to instantiate the client proxy class
IPQueryWebService.IpAddressSearchWebServiceSoapClient MyClient = new IPQueryWebService.IpAddressSearchWebServiceSoapClient ();
// Second step, bind callback event
MyClient.getCountryCityByIpCompleted + = (s, arg) =>
{
// get results
txbTip.Text = "Request completed.";
if (arg.Error! = null)
{
txtIP.Text = string.Format ("Error: {0}", arg.Error.Message);
return;
}
string [] res = arg.Result;
if (res! = null)
{
if (res.Length> 1)
{
txbResult.Text = string.Format ("Query result: {0}", res [1]);
}
}
};
// The third step is to call the asynchronous method
txbTip.Text = "Requesting, please wait ...";
MyClient.getCountryCityByIpAsync (txtIP.Text);
}
Well, complete. Remember that Web services are generally called asynchronously, so you need to get the call result in callback event processing.
Run the command and check the result.
OK.