我已经在我的必应地图上添加了MapPolyline。但是,该地图似乎只是吞没了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>


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

最佳答案

您必须先在e.Handled = true;函数中设置ExampleHandlerDown,以便它知道在处理了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映射的来源以查看其实际操作

09-12 09:30