【问题标题】:LeftMouseUp not firing - Bing Maps MapPolylineLeftMouseUp 未触发 - 必应地图 MapPolyline
【发布时间】:2019-05-22 16:07:00
【问题描述】:

我已将 MapPolyline 添加到我的 Bing 地图。但是,地图似乎只吞噬了 LeftMouseUp 事件。该事件适用于 MouseDown 甚至 RightMouseUp,但不适用于 LeftMouseUp。对于我的设计,我必须使用 LeftMouseUp 事件并且不能使用其他事件。为什么事件没有触发,我该如何解决?

public MainWindow()
{
    InitializeComponent();
    var testLine = new MapPolyline();
    var locations = new LocationCollection();
    locations.Add(new Location(50, 50));
    locations.Add(new Location(50, 60));
    locations.Add(new Location(50, 70));
    testLine.Locations = locations;
    testLine.Stroke = Brushes.Black;
    testLine.StrokeThickness = 15;
    testLine.PreviewMouseDown += ExampleHandlerDown;
    testLine.PreviewMouseUp += ExampleHandlerUp;
    MainMap.Children.Add(testLine);
}
private void ExampleHandlerDown(object sender, MouseEventArgs e)
{
    Console.WriteLine("Mouse Down");
}
private void ExampleHandlerUp(object sender, MouseEventArgs e)
{
    Console.WriteLine("Mouse Up");
}
<Window x:Class="StackQuestion.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
    mc:Ignorable="d"
    Title="MainWindow" Height="700" Width="1300">
<m:Map Name="MainMap" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CredentialsProvider="My Credentials"/>
</Window>

运行代码有以下结果: - 鼠标左键单击仅打印“鼠标向下” - 鼠标右键单击打印“鼠标向下”和“鼠标向上” 鼠标左键单击应与鼠标右键单击行为匹配。

【问题讨论】:

  • 尝试使用 PreviewMouseUp/Down。
  • 我刚做了,结果一样。

标签: c# wpf bing-maps


【解决方案1】:

您必须首先在您的ExampleHandlerDown 函数中设置e.Handled = true;,以便它知道在处理完Down 事件后调用MouseUp

private void ExampleHandlerDown(object sender, MouseEventArgs e)
{
    Console.WriteLine("Mouse Down");
    e.Handled = true;
}

为什么会这样?

不处理鼠标按下事件(尤其是预览鼠标按下事件)将导致鼠标按下事件传递到下一层,即地图;这意味着地图也会收到鼠标按下事件。当鼠标按下事件阻止您的鼠标向上工作时,地图正在做一些可疑的事情。

添加这些以了解我的意思

    MainMap.MouseDown += MainMap_MouseDown;
    MainMap.MouseUp += MainMap_MouseUp;

    private void MainMap_MouseUp(object sender, MouseButtonEventArgs e)
    {
        Console.WriteLine("Map Mouse Up");
    }

    private void MainMap_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Console.WriteLine("Map Mouse Down <-- Something going on in here");
    }

注释掉你的e.Handled = true,你会在这个输出中看到这个

Mouse Down
Map Mouse Down <-- Something going on in here
Map Mouse Up

把你的e.Handled = true 线放回去,你会看到这个输出

Mouse Down
Mouse Up
Map Mouse Up

因此,您会看到任何未处理的事件都被传递到地图。您可能也应该处理鼠标向上事件,以防止地图做任何您不想要的奇怪事情。我猜它在右键单击时不会做任何可疑的事情。您需要查看 bing 地图的来源才能了解它的实际作用

【讨论】:

  • 这非常有效。两个问题。 1. 为什么这个工作,以及 2. 为什么右键没有这个工作,而左边没有?
  • 我唯一的猜测是 bing 地图正在使用左键处理鼠标向上事件。也许是地图的一个错误。将鼠标向上处理程序添加到地图,您会明白我的意思,即它会触发而鼠标向上行不会触发,除非您处理鼠标向下事件。因此,向下处理鼠标可以防止地图做一些你不希望它做的事情,比如失去对线条的关注。我会更新答案以反映。
【解决方案2】:

正如您所说的那样,某些组件阻止了事件在您想要的地方进行处理。这是因为您正在使用 冒泡 事件 (MouseDown)。 WPF 提供了一个变体 PreviewMouseDown,它是一个 tunneling 事件。你可以阅读更多关于这两个here的区别。

基本上归结为,冒泡事件的处理顺序是从源元素开始向上通过每个父元素的可视化树。隧道事件是相反的——它们从视觉树中最高的元素开始,然后向任何触发它的元素移动。这实际上意味着,当您使用隧道事件时,任何阻止事件到达您想要的处理程序的东西都将被绕过,因为您是从不同的方向来的。

【讨论】:

  • 不幸的是,当我处理预览事件时也会发生同样的事情。我现在更新了问题中的代码以反映这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
相关资源
最近更新 更多