捕获元素的鼠标事件在拖动时停止触发

Mouse events for captured element stop firing while dragging

我有一些代码可以在屏幕上绘制一些点,然后允许在按住鼠标左键的同时拖动它们。除了不断地鼠标事件停止触发并且被拖动的点停止移动之外,这还挺有效。

因为所有事件都停止被捕获(出于某种未知原因),这意味着用户可以释放鼠标左键而不会发生任何事情。

奇怪的是,用户随后可以将鼠标重新定位在该点上,它会再次开始拖动,而无需按住鼠标左键。这会导致非常糟糕的用户体验。

这是怎么回事?

pinPoint.MouseLeftButtonDown += Point_MouseLeftButtonDown;
pinPoint.MouseLeftButtonUp += Point_MouseLeftButtonUp;
pinPoint.MouseMove += Point_MouseMove;

.
.
.

void Point_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    ((UIElement)sender).CaptureMouse();
    if (_isDragging == false)
    {
        _isDragging = true;
    }
}

void Point_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    _isDragging = false;
    ((UIElement)sender).ReleaseMouseCapture();
}

void Point_MouseMove(object sender, MouseEventArgs e)
{
    //where the point gets moved and some other logic
    //possibly this logic takes too long?
}

我找到了一个有效的解决方案。我仍然不知道为什么它会不断丢失 MouseCapture。

pinPoint.LostMouseCapture += Point_LostMouseCapture;

.
.
.

void Point_LostMouseCapture(object sender, MouseEventArgs e)
{
    //if we lost the capture but we are still dragging then just recapture it
    if (_isDragging)
    {
        ((UIElement)sender).CaptureMouse();
    }
}