延迟在的Windows Phone 8.1拖动/ UIElement的下降 [英] Delay in drag/drop of UIElement in Windows Phone 8.1

查看:138
本文介绍了延迟在的Windows Phone 8.1拖动/ UIElement的下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了在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时在用户之间的讨厌的延迟拖动开始时的椭圆实际上改变其位置。我可以打印到该事件处理程序本身完几乎立即执行的调试器看到的,所以我猜它有事情做在屏幕上的所有UI元素是造成这种延迟的预编程刷新率?

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

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的下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆