Windows Phone开发(22):启动器与选择器之BingMapsDirectionsTask .

来源:互联网 发布:免费阿里云服务器 编辑:程序博客网 时间:2024/05/16 04:58

从今天开发始,我们又开始新的征程,接下来的课程我们要熟悉一下启动器和选择器,其实二者是一样的,没有根本的区别,启动器是有返回结果的,如打开搜索应用程序进行搜索,而选择器是有返回内容的,如选择一张照片。

 

那么,启动器和选择器是啥玩意儿呢?其实我们可以很简单去理解,说白了,就是使用系自带的组件或应用程序。对的,就是这样,我说过,有时候很多概念只是名字上吓人罢了,实际用起来是非常简单的,比如这个启动器和选择器就是了。

 

到底是不是很简单,实践一下就知道了,本系列教程叫“轻松入门”,既然称得上是轻松,痛苦的事情不会叫大家去做,而MS一向注重用户体验,不会让大家痛苦的。

先来总结一下,使用启动器和选择器的方法是一样的,都是以下几步,不过选择器因为有返回内容,因此会多一步。

一、实例化组件,就是new一个;

二、设置相关参数或属性,比如你要打电话,你总得要设置一个号码吧,不然你打个鸟啊;

三、显示应用组件,既然调用了系统程序,让用户操作,当然要Show出来;

四、(可选)处理返回数据,这是选择器才有。

 

今天先讲第一个组件,BingMapsDirectionsTask,就是启动Bing地图对行车路线进行定位搜索,是啊,像导航系统吧?

 

有两种方法来使用该启动器,一是通过开始和结束标签,就是从哪里到哪里,如从武汉到上海,那么开始标签为Wuhan,结束标签为Shanghai;另一种方法是通开始和结束位置,如经度,纬度等。

 

首先,我们演示一下简单的,用标签来导航。

 

界面很简单了,相信通过前面的学习,大家都知道怎么弄了,只要能输入开始和结束标签即。

下面是后台C#代码:

 

[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. using Microsoft.Phone.Tasks;  
  14.   
  15. namespace LauncherSample  
  16. {  
  17.     public partial class MapByLabel : PhoneApplicationPage  
  18.     {  
  19.         public MapByLabel()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.   
  24.         private void button1_Click(object sender, RoutedEventArgs e)  
  25.         {  
  26.             BingMapsDirectionsTask map = new BingMapsDirectionsTask();  
  27.             map.Start = new LabeledMapLocation { Label = txtLabelStart.Text };  
  28.             map.End = new LabeledMapLocation { Label = txtLabelEnd.Text };  
  29.             map.Show();  
  30.         }  
  31.     }  
  32. }  


 

记得引入Microsoft.Phone.Tasks空间,所有的启动器和选择器都在里面。

 

 

 

好接下来,我们用能过经度和纬度来定位的方法。

 

首先要添加一个引用,在项目中右击“引用”,添加引用,然后选择System.Device,确定。

 

接着做好界面,同上需要开始的经度纬度,以及结束位置的经纬度。

 

 

然后就是代码。

 

[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. // 引入以下命名空间  
  14. using Microsoft.Phone.Tasks;  
  15. using System.Device.Location;  
  16.   
  17. namespace LauncherSample  
  18. {  
  19.     public partial class BingMapSample : PhoneApplicationPage  
  20.     {  
  21.         public BingMapSample()  
  22.         {  
  23.             InitializeComponent();  
  24.         }  
  25.   
  26.         private void button1_Click(object sender, RoutedEventArgs e)  
  27.         {  
  28.             BingMapsDirectionsTask bt = new BingMapsDirectionsTask();  
  29.             // 开始位置  
  30.             LabeledMapLocation locStart = new LabeledMapLocation();  
  31.             locStart.Location = new GeoCoordinate(Convert.ToDouble(txtLatitudeStart.Text), Convert.ToDouble(txtLongitudeStart.Text));  
  32.             // 结束位置  
  33.             LabeledMapLocation locEnd = new LabeledMapLocation();  
  34.             locEnd.Location = new GeoCoordinate(Convert.ToDouble(txtLatitudeEnd.Text), Convert.ToDouble(txtLongitudeEnd.Text));  
  35.             // 设置属性  
  36.             bt.Start = locStart;  
  37.             bt.End = locEnd;  
  38.             // 显示启动器  
  39.             bt.Show();  
  40.         }  
  41.     }  
  42. }