Windows Phone開發(22):啟動器與選取器之BingMapsDirectionsTask 轉:http://blog.csdn.net/tcjiaan/article/details/73

來源:互聯網
上載者:User

從今天開發始,我們又開始新的征程,接下來的課程我們要熟悉一下啟動器和選取器,其實二者是一樣的,沒有根本的區別,啟動器是有返回結果的,如開啟搜尋應用程式進行搜尋,而選取器是有返回內容的,如選擇一張照片。

 

那麼,啟動器和選取器是啥玩意兒呢?其實我們可以很簡單去理解,說白了,就是使用系內建的組件或應用程式。對的,就是這樣,我說過,有時候很多概念只是名字上嚇人罷了,實際用起來是非常簡單的,比如這個啟動器和選取器就是了。

 

到底是不是很簡單,實踐一下就知道了,本系列教程叫“輕鬆入門”,既然稱得上是輕鬆,痛苦的事情不會叫大家去做,而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. }  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.