2010-01-29 67 views

回答

21

未知,

這裏是一個一步一步後用於構建,顯示美國的兵圖的Silverlight應用程序,並增加了一個圖釘的每個點擊的位置。爲了好玩,當您瀏覽圖釘時,我添加了一些「懸停」功能。

步驟1:創建與Visual Studio(文件/新建工程/ Silverlight應用程序)的樣本Silverlight應用程序

步驟2:兩個兵DLL引用添加到Silverlight應用程序項目

Folder: C:\Program Files\Bing Maps Silverlight Control\V1\Libraries\ 
File 1: Microsoft.Maps.MapControl.dll 
File 2: Microsoft.Maps.MapControl.Common.dll 

步驟3:編輯MainPage.xaml中,並添加跟隨着在頂部克名稱空間:

xmlns:Maps="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl" 

步驟4:編輯MainPage.xaml中,並將其放置在用戶控件的網格內的以下代碼:

<Maps:Map x:Name="x_Map" Center="39.36830,-95.27340" ZoomLevel="4" /> 

步驟5:編輯的MainPage。 CS,並添加以下using語句:

using Microsoft.Maps.MapControl; 

步驟6:編輯MainPage.cs,並用下面的代碼替換MainPage類:

public partial class MainPage : UserControl 
{ 
    private MapLayer m_PushpinLayer; 

    public MainPage() 
    { 
     InitializeComponent(); 
     base.Loaded += OnLoaded; 
    } 

    private void OnLoaded(object sender, RoutedEventArgs e) 
    { 
     base.Loaded -= OnLoaded; 

    m_PushpinLayer = new MapLayer(); 
    x_Map.Children.Add(m_PushpinLayer); 
     x_Map.MouseClick += OnMouseClick; 
    } 

    private void AddPushpin(double latitude, double longitude) 
    { 
     Pushpin pushpin = new Pushpin(); 
     pushpin.MouseEnter += OnMouseEnter; 
     pushpin.MouseLeave += OnMouseLeave; 
     m_PushpinLayer.AddChild(pushpin, new Location(latitude, longitude), PositionOrigin.BottomCenter); 
    } 

    private void OnMouseClick(object sender, MapMouseEventArgs e) 
    { 
     Point clickLocation = e.ViewportPoint; 
     Location location = x_Map.ViewportPointToLocation(clickLocation); 
     AddPushpin(location.Latitude, location.Longitude); 
    } 

    private void OnMouseLeave(object sender, MouseEventArgs e) 
    { 
     Pushpin pushpin = sender as Pushpin; 

     // remove the pushpin transform when mouse leaves 
     pushpin.RenderTransform = null; 
    } 

    private void OnMouseEnter(object sender, MouseEventArgs e) 
    { 
     Pushpin pushpin = sender as Pushpin; 

     // scaling will shrink (less than 1) or enlarge (greater than 1) source element 
     ScaleTransform st = new ScaleTransform(); 
     st.ScaleX = 1.4; 
     st.ScaleY = 1.4; 

     // set center of scaling to center of pushpin 
     st.CenterX = (pushpin as FrameworkElement).Height/2; 
     st.CenterY = (pushpin as FrameworkElement).Height/2; 

     pushpin.RenderTransform = st; 
    } 
} 

步驟7:構建和運行!

乾杯,吉姆·麥柯迪

Face To Face SoftwareYinYangMoney

+0

我沒有看到工作圖釘 - 你怎麼掛鉤的事件? – Tim 2011-11-02 03:25:41

+0

事件在步驟6開始時連接起來。 – 2012-11-28 05:39:12