Windows Phone 上的 Web 服务






4.97/5 (11投票s)
在 Windows Phone 中使用 Web 服务
引言
移动设备正变得越来越联网。 这鼓励开发者开发利用此优势的应用程序。 Web 服务是这些应用程序中最重要的一部分。 如今,Web 服务是每个开发人员都必须了解的技术。
要求
由于这是一个 Windows Phone 应用程序,您需要安装 Visual Studio for Windows Phone Express 或 Visual Studio 2010,您可以从以下位置下载:
关于此应用程序
此应用程序将使用 Bing 地图来显示地图上两点之间的路线并显示其长度。 由于我将更多地关注 Web 服务,您可以找到 Bing Maps 和 Bing Maps v2 来帮助您开始操作 Bing 地图。Windows Phone 中的 Bing 地图 是一篇简单的入门文章。
假设您已将 Map 控件从工具箱拖放到主页,并插入了与 ApplicationBar
相关的以下代码
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True"
IsMenuEnabled="True" Opacity="0.9">
<shell:ApplicationBarIconButton IconUri="/Icons/plus.png"
Text="Zoom In" Click="zoomIn_click"/>
<shell:ApplicationBarIconButton IconUri="/Icons/minus.png" Text="Zoom out"
Click="zoomOut_click"/>
<shell:ApplicationBarIconButton IconUri="/Icons/A.png"
Text="Aerial mode" Click="Aerial_click"/>
<shell:ApplicationBarIconButton IconUri="/Icons/R.png"
Text="Road mode" Click="Road_click"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="Choose my position"
Click="chooseMyPosition_click"/>
<shell:ApplicationBarMenuItem Text="Locate Me" Click="locateMe_click"/>
<shell:ApplicationBarMenuItem Text="Set Pushpin" Click="setPin_click"/>
<shell:ApplicationBarMenuItem Text="Add Pushpin" Click="addPin_click"/>
<shell:ApplicationBarMenuItem Text="Show Route" Click="showRoute_click"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
让我们添加这些 using
语句以使用 Maps
功能
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
之后,我们将能够编写此代码
//declaring the two Pushpins and the Polyline
Pushpin pin1 = new Pushpin();
Pushpin pin2 = new Pushpin();
MapPolyline poly = new MapPolyline();
需要进行一些初始化才能在 Map
中显示 Polyline
//Initialization for polyline
poly.Locations = new LocationCollection();
poly.Opacity = 1.0;
poly.StrokeThickness = 3;
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Colors.Green;
poly.Stroke = mySolidColorBrush;
现在,我们将开始使用 Web 服务。 因此,首先,让我们右键单击我们的项目,然后单击“添加服务引用”
在地址字段中,粘贴此对路由 Web 服务的引用 http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc/mex 并单击“转到”,然后这些服务将出现在“Services
”字段中,以证明它不是错误的引用。
单击“确定”按钮后,将在您的项目中生成一些文件。 所以现在,我们可以通过它的引用来使用该分布式服务。
private void showRoute_click(object sender, EventArgs e)
{
//creating the reference to the service
ServiceReference1.RouteServiceClient proxy =
new ServiceReference1.RouteServiceClient("BasicHttpBinding_IRouteService");
ServiceReference1.RouteRequest rr = new ServiceReference1.RouteRequest();
//this service requires a key
rr.Credentials = new ServiceReference1.Credentials();
rr.Credentials.ApplicationId =
"Asa2x7ZzhYIHauji6TzIkcf3TIDznTgBaPKQehsyE4taOz19Mx4fP4lyihqbTj7D";
rr.Options = new ServiceReference1.RouteOptions();
rr.Options.RoutePathType = ServiceReference1.RoutePathType.Points;
//declaring the two points
ServiceReference1.Waypoint wp1 = new ServiceReference1.Waypoint();
ServiceReference1.Waypoint wp2 = new ServiceReference1.Waypoint();
//the first location is my location (from)
wp1.Location = new ServiceReference1.Location();
wp1.Location.Latitude = SharedInformation.myLatitude;
wp1.Location.Longitude = SharedInformation.myLongitude;
wp1.Description = "";
//the second location is the destination (to)
wp2.Location = new ServiceReference1.Location();
wp2.Location.Latitude = SharedInformation.pinLat;
wp2.Location.Longitude = SharedInformation.pinLong;
wp2.Description = "";
//setting the parameter to send
rr.Waypoints =
new System.Collections.ObjectModel.ObservableCollection<ServiceReference1.Waypoint>();
rr.Waypoints.Add(wp1);
rr.Waypoints.Add(wp2);
//invoking the web service
proxy.CalculateRouteAsync(rr);
proxy.CalculateRouteCompleted +=
new EventHandler<ServiceReference1.CalculateRouteCompletedEventArgs>
(proxy_CalculateRouteCompleted);
}
非常重要的是要知道,Windows Phone 中的 Web 服务是以异步方式调用的。 这就是为什么表达式“Async
”被添加到方法名称的末尾的原因。 这样可以避免接口的线程被阻塞。
IntelliSence 可以帮助您显示可以从 Web 服务调用的不同方法及其完整签名,如此处所示
此 Web 服务返回的结果通过 proxy_CalculateRouteCompleted
方法的第二个参数返回,该方法将在收到来自服务的响应时执行。 此结果是一个点列表。 将一个链接到下一个,它们将绘制我们需要使用折线显示的路线。
public void proxy_CalculateRouteCompleted
(object obj, ServiceReference1.CalculateRouteCompletedEventArgs e)
{
try
{
foreach (ServiceReference1.Location location in e.Result.Result.RoutePath.Points)
{
poly.Locations.Add(new GeoCoordinate(location.Latitude, location.Longitude));
}
//add the Polyline to the Map
map1.Children.Add(poly);
//display the distance between the two Pushpins
pin2.Content = "It's " + e.Result.Result.Summary.Distance.ToString() + "Km far away!";
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
使用 try catch
块是因为该服务可能会返回一些异常,例如 Point
离任何道路太远。
最终应用程序的一些屏幕截图
结果将是这样
我希望你喜欢我的文章。
如果您需要更多信息,请告诉我。