c# - Silverlight - 通过 C# 将文本添加到 Bing map 中的图钉

标签 c# .net silverlight bing-maps bing

我能够让我的 silverlight Bing map 接受鼠标点击并将它们转换为 C# 中的图钉。现在我想在 PushPin 旁边显示一个文本作为鼠标经过图钉时出现的描述,我不知道该怎么做。有什么方法可以让我做这件事?

这是 C# 代码:

public partial class MainPage : UserControl

{ 私有(private) map 层 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;
}

最佳答案

您有 2 条路可走:

(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) 创建一个 native PushPin 对象以传递给 PushpinLayer.AddChild,但首先设置它的 Template 属性。请注意,PushPin 是 ContentControl,并且具有可从 XAML 中定义的资源中设置的模板属性:

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>

祝你好运, 吉姆麦柯迪

面对面软件和阴阳钱

关于c# - Silverlight - 通过 C# 将文本添加到 Bing map 中的图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2805355/

相关文章:

silverlight - Telerik Silverlight RadTreeview 自动刷新

c# - 如何将java代码与c#代码连接起来?

c# - 解析数学表达式

.net - Azure AD Microsoft 帐户客户端是公共(public)的,因此不应显示 'client_assertion' 和 'client_secret'

c# - 学习创建自定义 Silverlight 组件

c# - Silverlight 和一个表单应用程序

c# - 是否可以将 xml 中的字符串值强制为 bool 值?

c# - 为什么编译器允许将 Convert.ToString() 分配给整数?

c# - 使用实体检索 SQL View

.net - 代码契约 Vs。对象初始值设定项 (.net 4.0)