簡體   English   中英

Silverlight - 通過C#向Bing Maps中的圖釘添加文本

[英]Silverlight - Adding Text to Pushpin in Bing Maps via C#

我能夠使我的Silverlight Bing地圖接受Mousclicks並將它們轉換為C#中的Pushpins。 現在我想在PushPin旁邊顯示一個文本作為鼠標越過引腳時出現的描述,我不知道如何做到這一點。 有什么方法可以讓我做這件事?

這是C#代碼:

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;
}

}

你有兩種方法:

(1)創建任何UIElement以傳遞到PushPinLayer.AddChild。 AddChild方法將接受任何UIElement,例如包含Image和TextBlock的Grid:

MapLayer m_PushpinLayer = new MapLayer(); 
Your_Map.Children.Add(m_PushpinLayer);
Image image = new Image(); 
image.Source = ResourceFile.GetBitmap("Images/Pushpin.png", From.This); 
TextBlock textBlock = new TextBlock();
textBlock.Text = "My Pushpin";
Grid grid = new Grid();
grid.Children.Add(image);
grid.Children.Add(textBlock);

m_PushpinLayer.AddChild(grid, 
    new Microsoft.Maps.MapControl.Location(42.658, -71.137),   
        PositionOrigin.Center);

(2)創建一個本機PushPin對象以傳遞到PushpinLayer.AddChild,但首先設置它的Template屬性。 請注意,PushPin是ContentControls,並且具有可以從XAML中定義的資源設置的Template屬性:

MapLayer m_PushpinLayer = new MapLayer(); 
Your_Map.Children.Add(m_PushpinLayer); 
Pushpin pushpin = new Pushpin(); 
pushpin.Template = Application.Current.Resources["PushPinTemplate"]   
    as (ControlTemplate); 
m_PushpinLayer.AddChild(pushpin, 
    new Microsoft.Maps.MapControl.Location(42.658, -71.137),   
        PositionOrigin.Center);

...

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
> 
    <ControlTemplate x:Key="PushPinTemplate"> 
        <Grid> 
            <Image Source="Images/Pushpin.png" /> 
            <TextBlock Text="My Pushpin" /> 
        </Grid> 
    </ControlTemplate> 
</ResourceDictionary>

祝你好運,Jim McCurdy

面對面軟件和YinYangMoney

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM