调用缩放功能图,与WPF中的MouseWheel事件相关
Call zoom function map, related with MouseWheel event in WPF
我有 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 事件时遇到一些问题。
如何从子项(图钉或其他)调用 Map MouseWheel 事件??
要在地图上引发 MouseWheelEvent,您可以声明一个新的事件处理程序,它将事件转发给 map.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;
我有 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 事件时遇到一些问题。
如何从子项(图钉或其他)调用 Map MouseWheel 事件??
要在地图上引发 MouseWheelEvent,您可以声明一个新的事件处理程序,它将事件转发给 map.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;