Avalonia UI中PointerMovedEvent指针位置仅更新一次的问题求助
Avalonia UI中PointerMovedEvent指针位置仅更新一次的问题求助
大家好,我在Avalonia UI里实现了一个自定义的瞬时按钮——设计逻辑是按下时激活,松开后恢复inactive状态。在PC端用鼠标操作时一切正常,但在触摸设备上碰到了麻烦:如果我在按钮区域内按下,然后拖动到按钮外面再松开,按钮会一直保持激活状态,完全没响应松开事件。
为了解决这个问题,我尝试追踪指针位置,打算在指针移出按钮区域时手动触发释放操作,但发现PointerMovedEvent只在第一次移动时更新了指针位置,后续的移动事件里,获取到的位置始终不变。
以下是我的实现代码:
public MomentaryButton() { AddHandler(PointerPressedEvent, OnPress, RoutingStrategies.Tunnel); AddHandler(PointerReleasedEvent, OnRelease, RoutingStrategies.Tunnel); AddHandler(PointerCaptureLostEvent, OnRelease, RoutingStrategies.Tunnel); AddHandler(PointerMovedEvent, OnPointerMoved, RoutingStrategies.Tunnel); } private void OnPress(object? sender, PointerPressedEventArgs e) { if (Disabled) return; PressInternal(); e?.Pointer.Capture(this); } private void OnRelease(object? sender, PointerEventArgs e) { if (!IsActive) return; ReleaseInternal(); e?.Pointer.Capture(null); } private void OnPointerMoved(object? sender, PointerEventArgs e) { if (!IsActive) return; Point point = e.GetCurrentPoint(this).Position; Log.Debug("Pointer moved at position: {Position}", point); }
这是触摸移动时的日志输出,能看到只有最后一条位置有变化,前面的都完全相同:
2025-08-26 10:05:08.690 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.696 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.700 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.706 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.711 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.716 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.721 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.726 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.731 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.736 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.741 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.746 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.751 +02:00 [DBG] Pointer moved at position: 69, 95 2025-08-26 10:05:08.756 +02:00 [DBG] Pointer moved at position: 68, 86
有没有大佬遇到过类似的问题?或者能指点下为什么指针位置不会持续更新,以及怎么解决触摸设备上按钮的这个状态问题?
内容来源于stack exchange




