WPF:通过触摸拖动元素非常不稳定

问题描述 投票:1回答:1

我有一个可以在某些边界内拖动的矩形。这与鼠标完美搭配。

我将IsManipulationEnabled设置为true时,鼠标事件不再起作用。但是我需要此来获取矩形上的触摸事件。因此,我将其设置为true。

我正在尝试计算ManipulationDelta事件中的所有变化,如以下函数所示。缩放效果已经很好,但是通过用手指拖动对象来移动对象非常不稳定+有时Rectangle来回跳跃。

private void UserControl_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    //Scaling works already pretty good
    RangeBar.Width *= e.DeltaManipulation.Scale.X;

    //Moving the element is very choppy, what am I doing wrong here?
    this.startX = this.startX + e.DeltaManipulation.Translation.X;
    RangeBar.SetValue(Canvas.LeftProperty, startX);
}
c# .net wpf multi-touch
1个回答
1
投票

我尝试改用CumulativeManipulation。

[每当我需要通过拖动来进行UI元素移动时,我都不会尝试重用相同的变量并通过增量对其进行修改,然后再将相同的变量用于设置位置。无论平台如何,它几乎总是给我带来稳定性问题。相反,尝试在拖动开始时存储变量,并仅在需要更新位置时才将增量添加到该变量中。所以更像这样:

Point origin;

void MouseDown(Point location)
{
   origin = location;
}

void MouseDrag(Vector cumulativeOffset)
{
   SetControlLocation(origin+cumulativeOffset);
}

而且,ManipulationEvent的来源是什么?您肯定要确保它不是当前的矩形,否则肯定会引起您所看到的问题。

© www.soinside.com 2019 - 2024. All rights reserved.