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

 

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

 

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

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

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

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

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

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

 

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

 

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

 

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

 

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

下面是后台C#代码:

 

using System;using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using Microsoft.Phone.Tasks; namespace LauncherSample { public partial class MapByLabel : PhoneApplicationPage { public MapByLabel() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { BingMapsDirectionsTask map = new BingMapsDirectionsTask(); map.Start = new LabeledMapLocation { Label = txtLabelStart.Text }; map.End = new LabeledMapLocation { Label = txtLabelEnd.Text }; map.Show(); } } }

 

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

 

 

 

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

 

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

 

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

 

 

然后就是代码。

 

using System;using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; // 引入以下命名空间 using Microsoft.Phone.Tasks; using System.Device.Location; namespace LauncherSample { public partial class BingMapSample : PhoneApplicationPage { public BingMapSample() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { BingMapsDirectionsTask bt = new BingMapsDirectionsTask(); // 开始位置 LabeledMapLocation locStart = new LabeledMapLocation(); locStart.Location = new GeoCoordinate(Convert.ToDouble(txtLatitudeStart.Text), Convert.ToDouble(txtLongitudeStart.Text)); // 结束位置 LabeledMapLocation locEnd = new LabeledMapLocation(); locEnd.Location = new GeoCoordinate(Convert.ToDouble(txtLatitudeEnd.Text), Convert.ToDouble(txtLongitudeEnd.Text)); // 设置属性 bt.Start = locStart; bt.End = locEnd; // 显示启动器 bt.Show(); } } }