Silverlight - 通过C#向Bing地图的Pushpin添加文本

6

我可以在C#中使我的Silverlight Bing地图接受鼠标点击并将其转换为Pushpin。现在我想在Pushpin旁边显示一个文本作为描述,当鼠标悬停在标记上时出现,我不知道如何做到这一点。有哪些方法可以让我完成这件事?

以下是C#代码:

public partial class MainPage : UserControl

私有变量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个回答

7

你有两种方法可选:

(1) 创建任何UIElement并将其传递到PushPinLayer.AddChild中。AddChild方法将接受任何UIElement,例如包含图像和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是ContentControl,具有可以从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


谢谢Jim,非常有帮助 :) 一开始它没有起作用...直到我把grid.Add改成了grid.Children.Add...再次感谢! - Morano88

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接