左侧鼠标向上未触发-Bing地图映射折线 [英] LeftMouseUp not firing - Bing Maps MapPolyline

查看:0
本文介绍了左侧鼠标向上未触发-Bing地图映射折线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将地图折线添加到我的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>

运行代码会产生以下结果: -鼠标左键点击仅打印"Mouse Down"(鼠标按下) -鼠标右键点击会同时打印"Mouse Down"和"Mouse Up" 鼠标左键单击应与鼠标右键单击行为匹配。

推荐答案

您必须先在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地图的源代码才能了解它的实际功能

这篇关于左侧鼠标向上未触发-Bing地图映射折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆