使用 Reactive Extensions 检测 MouseDown 和 MouseMove 事件
Detect MouseDown and MouseMove events with Reactive Extensions
我有以下代码来侦听从 WPF ListView 开始的拖放事件。
这个想法是鼠标按下事件将跟随鼠标移动。一旦鼠标移动超过最小距离,拖放动作将开始。
var mouseDowns = Observable.FromEventPattern<MouseEventArgs>(this, "PreviewMouseDown");
var mouseMoves = Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove").
Where(
x =>
x.EventArgs.LeftButton == MouseButtonState.Pressed ||
x.EventArgs.RightButton == MouseButtonState.Pressed);
var drag = from mouseDown in mouseDowns
from mouseMove in mouseMoves
let initialPosition = mouseDown.EventArgs.GetPosition(null)
let currentPosition = mouseMove.EventArgs.GetPosition(null)
let mouseDifference = initialPosition - currentPosition
//where
// (Math.Abs(mouseDifference.X) > MinimumDragDistance ||
// Math.Abs(mouseDifference.Y) > MinimumDragDistance)
select mouseDifference;
dragSubscription = drag.Subscribe(_ =>
{
Debug.WriteLine(string.Format("x: {0} y: {1}", _.X, _.Y));
var dataObject = GetDataObject();
DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Move);
});
问题在于,在 Linq 中,每当触发满足条件的 PreviewMouseDown 事件时,只会捕获一个 mouseMove 事件,这意味着当我在列表中包含 where
语句时,mouseDifference 总是0,不符合条件
有没有办法允许多个 mouseMove 事件发生,直到符合条件的事件发生?
我已经能够使用以下问题中的代码示例回答问题:
Using Rx over events on a WPF UserControl, why does the control receive a mousedown and mousemove when the window is maximized?
我有以下代码来侦听从 WPF ListView 开始的拖放事件。 这个想法是鼠标按下事件将跟随鼠标移动。一旦鼠标移动超过最小距离,拖放动作将开始。
var mouseDowns = Observable.FromEventPattern<MouseEventArgs>(this, "PreviewMouseDown");
var mouseMoves = Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove").
Where(
x =>
x.EventArgs.LeftButton == MouseButtonState.Pressed ||
x.EventArgs.RightButton == MouseButtonState.Pressed);
var drag = from mouseDown in mouseDowns
from mouseMove in mouseMoves
let initialPosition = mouseDown.EventArgs.GetPosition(null)
let currentPosition = mouseMove.EventArgs.GetPosition(null)
let mouseDifference = initialPosition - currentPosition
//where
// (Math.Abs(mouseDifference.X) > MinimumDragDistance ||
// Math.Abs(mouseDifference.Y) > MinimumDragDistance)
select mouseDifference;
dragSubscription = drag.Subscribe(_ =>
{
Debug.WriteLine(string.Format("x: {0} y: {1}", _.X, _.Y));
var dataObject = GetDataObject();
DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Move);
});
问题在于,在 Linq 中,每当触发满足条件的 PreviewMouseDown 事件时,只会捕获一个 mouseMove 事件,这意味着当我在列表中包含 where
语句时,mouseDifference 总是0,不符合条件
有没有办法允许多个 mouseMove 事件发生,直到符合条件的事件发生?
我已经能够使用以下问题中的代码示例回答问题:
Using Rx over events on a WPF UserControl, why does the control receive a mousedown and mousemove when the window is maximized?