Map MouseLeftButtonUp doesn't fire always

Viewed 156

I'm using BingMaps to make an app that allows to draw shapes over a map. In this code I need to listen for MouseLeftButtonUp and MouseLeftButtonDown in order to understand when the person starts and stops drawing. But for some odd reason the MouseLeftButtonUp doesn't fire always. In this app users can draw freely or shapes, when we draw freely the handler is called but when it is a shape (eg. rectangle) it doesn't get called always. I've searched already and the solution presented for this question doesn't seem to work for me. Also tried using tunneling events but didn't work either.

Here's my Map.xaml:

<Window x:Class="Satellite_Vessel_Tracker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
        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:local="clr-namespace:Satellite_Vessel_Tracker"
        mc:Ignorable="d"
        Title="Satellite Vessel Tracker" Height="550" Width="940" WindowStartupLocation="CenterScreen" MinWidth="940" MinHeight="550" MaxHeight="550" MaxWidth="940">
    <m:Map Name="MainMap" CredentialsProvider="..." MouseMove="MainMap_MouseMove" MouseLeftButtonDown="MainMap_MouseLeftButtonDown" MouseLeftButtonUp="MainMap_MouseLeftButtonUp" Margin="0,0,408.6,-0.4" Height="490"/>

</Window>   

And here is the code for drawing lines over the map:

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Maps.MapControl.WPF;

namespace Line
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private readonly int StrokeThickness = 2;
        private UIElement last;
        Point start, end;

        private void MainMap_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
            {
                last = null;
                start = e.GetPosition(MainMap);
                e.Handled = true;
            }
        }

        private void MainMap_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("MouseButtonUp");
            end = e.GetPosition(MainMap);
            UIElement shape = DrawLine();
            MainMap.Children.Add(shape);
            last = null;
            e.Handled = true;
        }

        private void MainMap_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                MainMap.Children.Remove(last);
                end = e.GetPosition(MainMap);
                last = DrawLine();
                MainMap.Children.Add(last);
                e.Handled = true;
            }
        }

        private UIElement DrawLine()
        {
            MapPolyline line = new MapPolyline()
            {
                Locations = new LocationCollection()
                {
                    MainMap.ViewportPointToLocation(start),
                    MainMap.ViewportPointToLocation(end)
                },
                Stroke = Brushes.Black,
                StrokeThickness = StrokeThickness
            };
            return line;
        }
    }
}

UPDATE:

  • As stated in one of the comments I've tried using the AddHandler method with the handledEventsToo flag although it still doesn't get fired all the times.

  • Also this happens even when the mouse is within the bounds of the map, as tested with MouseLeave.

  • PreviewMouseLeftButtonUp doesn't get fired everytime either.

0 Answers
Related