我有WPF应用和Bing地图,我一时陷入困境。按住鼠标左键可创建一个图钉。而且,如果您想在光标位于插入的图钉上时使用鼠标滚轮更改地图滚动,则不会发生任何事情。我了解,我在地图上的物品没有通话事件。但是如果使用MouseUp事件,我将重新定义我的方法(函数:myMap_MouseMove,str:pin.MouseUp + = Pin_MouseUp;)]
代码(XAML):
<m:Map x:Name="myMap" ZoomLevel="15" Mode="AerialWithLabels" MouseMove="myMap_MouseMove" MouseUp="myMap_MouseUp" />
代码(C#):
private void myMap_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (isDragging)
{
isDragging = false;
return;
}
switch (e.ChangedButton)
{
case System.Windows.Input.MouseButton.Left:
myMap_MouseLeftButtonDown(sender, e);
break;
case System.Windows.Input.MouseButton.Right:
myMap_MouseRightButtonDown(sender, e);
break;
default:
break;
}
}
private void myMap_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
...
foreach (var viewportPoint in drawPolyPoints)
{
// Convert the mouse coordinates to a location on the map
ControlTemplate template = (ControlTemplate)this.FindResource("CutomPushpinTemplate");
Pushpin pin = new Pushpin();
pin.Location = viewportPoint;
//pin.Template = template;
pin.Style = (Style)this.FindResource("LabelPushpinStyle");
pin.MouseUp += Pin_MouseUp; // It's OK
pin.MouseWheel += myMap.MouseWheel; // problem
myMap.Children.Add(pin);
}
double area = CalculateArea(_polyLocations);
this.Fields.Text = "Area: " + area;
}
private void myMap_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
...
e.Handled = true;
Point mousePosition = e.GetPosition(myMap);
Location pinLocation = myMap.ViewportPointToLocation(mousePosition);
ControlTemplate template = (ControlTemplate)this.FindResource("CutomPushpinTemplate");
// The pushpin to add to the map.
Pushpin pin = new Pushpin();
pin.Location = pinLocation;
pin.Style = (Style)this.FindResource("LabelPushpinStyle");
// Adds the pushpin to the map
myMap.Children.Add(pin);
drawPolyPoints.Add(pin.Location);
...
}
我在MouseWheel事件中遇到一些问题。
如何调用子项(PushPin或其他)中的Map MouseWheel事件?
要在地图上引发MouseWheelEvent,可以声明一个新的eventHandler,它将把事件转发到地图上。MouseWheelEvent处理程序:
private void RaiseMyMapMouseWheel(object sender, MouseWheelEventArgs e)
{
var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
{
RoutedEvent = UIElement.MouseWheelEvent,
//You can change the sender for myMap if you wish
Source = sender
};
myMap.RaiseEvent(eventArg);
}
然后您像这样添加处理程序:
pin.MouseWheel += this.RaiseMyMapMouseWheel;