Windows Phone開發(fā)(22):啟動(dòng)器與選擇器之一
從今天開發(fā)始,我們又開始新的征程,接下來的課程我們要熟悉一下啟動(dòng)器和選擇器,其實(shí)二者是一樣的,沒有根本的區(qū)別,啟動(dòng)器是有返回結(jié)果的,如打開搜索應(yīng)用程序進(jìn)行搜索,而選擇器是有返回內(nèi)容的,如選擇一張照片。
那么,啟動(dòng)器和選擇器是啥玩意兒呢?其實(shí)我們可以很簡單去理解,說白了,就是使用系自帶的組件或應(yīng)用程序。對(duì)的,就是這樣,我說過,有時(shí)候很多概念只是名字上嚇人罷了,實(shí)際用起來是非常簡單的,比如這個(gè)啟動(dòng)器和選擇器就是了。
到底是不是很簡單,實(shí)踐一下就知道了,本系列教程叫“輕松入門”,既然稱得上是輕松,痛苦的事情不會(huì)叫大家去做,而MS一向注重用戶體驗(yàn),不會(huì)讓大家痛苦的。
先來總結(jié)一下,使用啟動(dòng)器和選擇器的方法是一樣的,都是以下幾步,不過選擇器因?yàn)橛蟹祷貎?nèi)容,因此會(huì)多一步。
一、實(shí)例化組件,就是new一個(gè);
二、設(shè)置相關(guān)參數(shù)或?qū)傩裕热缒阋螂娫?,你總得要設(shè)置一個(gè)號(hào)碼吧,不然你打個(gè)鳥??;
三、顯示應(yīng)用組件,既然調(diào)用了系統(tǒng)程序,讓用戶操作,當(dāng)然要Show出來;
四、(可選)處理返回?cái)?shù)據(jù),這是選擇器才有。
今天先講第一個(gè)組件,BingMapsDirectionsTask,就是啟動(dòng)Bing地圖對(duì)行車路線進(jìn)行定位搜索,是啊,像導(dǎo)航系統(tǒng)吧?
有兩種方法來使用該啟動(dòng)器,一是通過開始和結(jié)束標(biāo)簽,就是從哪里到哪里,如從武漢到上海,那么開始標(biāo)簽為Wuhan,結(jié)束標(biāo)簽為Shanghai;另一種方法是通開始和結(jié)束位置,如經(jīng)度,緯度等。
首先,我們演示一下簡單的,用標(biāo)簽來導(dǎo)航。
界面很簡單了,相信通過前面的學(xué)習(xí),大家都知道怎么弄了,只要能輸入開始和結(jié)束標(biāo)簽即。
下面是后臺(tái)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空間,所有的啟動(dòng)器和選擇器都在里面。
好接下來,我們用能過經(jīng)度和緯度來定位的方法。
首先要添加一個(gè)引用,在項(xiàng)目中右擊“引用”,添加引用,然后選擇System.Device,確定。
接著做好界面,同上需要開始的經(jīng)度緯度,以及結(jié)束位置的經(jīng)緯度。
然后就是代碼。
- 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));
- // 結(jié)束位置
- LabeledMapLocation locEnd = new LabeledMapLocation();
- locEnd.Location = new GeoCoordinate(Convert.ToDouble(txtLatitudeEnd.Text), Convert.ToDouble(txtLongitudeEnd.Text));
- // 設(shè)置屬性
- bt.Start = locStart;
- bt.End = locEnd;
- // 顯示啟動(dòng)器
- bt.Show();
- }
- }
- }