我正在使用 ManipulationDelta
事件处理程序将一个简单的椭圆拖放到屏幕上的画布中.我正在使用在几个地方在线发布的标准方法.以下是我的事件处理程序中的代码:
I'm using the ManipulationDelta
event handler to drag and drop a simple ellipse in a canvas across the screen. I'm using the standard approach posted online in several places. Following is the code in my event handler:
Ellipse dragableItem = sender as Ellipse;
TranslateTransform translateTransform = dragableItem.RenderTransform as TranslateTransform;
double newPosX = Canvas.GetLeft(dragableItem) + translateTransform.X + e.Delta.Translation.X;
double newPosY = Canvas.GetTop(dragableItem) + translateTransform.Y + e.Delta.Translation.Y;
if (!isCanvasBoundary(newPosX, TestCanvas.ActualWidth - dragableItem.ActualWidth, 0))
translateTransform.X += e.Delta.Translation.X;
if (!isCanvasBoundary(newPosY, TestCanvas.ActualHeight - dragableItem.ActualHeight, 0))
translateTransform.Y += e.Delta.Translation.Y;
拖放操作工作正常,但在用户开始拖动到椭圆实际改变其位置之间存在大约 1 秒的延迟.我可以通过打印到调试器看到事件处理程序本身几乎立即完成执行,所以我猜它有一些事情可以为屏幕上的所有 UIElements 执行预编程的刷新率,从而导致这种延迟?
The drag and drop operation works fine, but there's a nasty delay of around 1 second between when the user begins dragging to when the ellipse actually changes its position. I can see by printing to the Debugger that the event handler itself finishes executing almost instantly, so I'm guessing it has something to do a pre-programmed refresh rate for all UIElements on the screen that's causing this delay?
这个问题有什么办法吗?
Is there anyway around this issue?
我前段时间遇到了同样的问题.我想延迟是决定手势是拖动还是点击.如果不意外拖动,很难触摸屏幕.
I had the same problem some time ago. I guess that the delay is to decide whether the gesture is drag or tap. It's hard to touch a screen without any accidental drag.
要消除这种滞后,您可以使用 PointerMove
和 PointerPressed
事件.这是我的例子.你有两个椭圆的画布,可以毫不拖延地拖动.
To eliminate this lag, you can use PointerMove
and PointerPressed
events. Here's my example. You have canvas with two ellipses which can be dragged without any delay.
XAML
<Grid>
<Canvas x:Name="Board" PointerMoved="Canvas_PointerMoved" Background="Black">
<Ellipse Width="64" Height="64" Fill="Red"
Canvas.Left="32" Canvas.Top="128" PointerPressed="Ellipse_PointerPressed"/>
<Ellipse Width="96" Height=" 96" Fill="Blue"
Canvas.Left="128" Canvas.Top="16" PointerPressed="Ellipse_PointerPressed"/>
</Canvas>
</Grid>
如您所见,我正在处理画布中的 PointerMoved
事件和椭圆中的 PointerPressed
事件.重要的是画布的背景不透明以处理触摸事件.
As you can see, I'm handling PointerMoved
event in canvas and PointerPressed
event in ellipses. It's important that the background of the canvas is not transparent to handle touch events.
C#
public sealed partial class MainPage : Page
{
UIElement draggedItem = null;
Point offset;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
private void Ellipse_PointerPressed(object sender, PointerRoutedEventArgs e)
{
draggedItem = sender as UIElement;
offset = e.GetCurrentPoint(draggedItem).Position;
}
private void Canvas_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (draggedItem == null)
return;
Point dragPoint = e.GetCurrentPoint(Board).Position;
Canvas.SetLeft(draggedItem, dragPoint.X - offset.X);
Canvas.SetTop(draggedItem, dragPoint.Y - offset.Y);
}
}
我认为代码非常简单易懂.我使用 PointerPressed
来决定拖动哪个对象.我也在计算一些偏移量,因为我们想要相对于用户触摸的点移动对象.
I think the code is quite simple and understandable. I use PointerPressed
to decide which object is dragged. I'm also calculating some offset, because we want to move the object relative to the point where user touches.
这篇关于Windows Phone 8.1 中 UIElement 的拖放延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!