我能够使我的Silverlight Bing地图接受Mousclicks并将它们转换为C#中的Pushpins。现在我想在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;
}
}
答案 0 :(得分:7)
您有两种方法可以:
(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>
祝你好运,
吉姆麦克库迪
面对面软件和YinYangMoney