在Windows Phone 8.1中拖放UIElement [英] Delay in drag/drop of UIElement in Windows Phone 8.1

查看:177
本文介绍了在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秒的令人讨厌的延迟当椭圆实际上改变其位置时开始拖动。我可以通过打印到调试器看到,事件处理程序本身几乎立即完成执行,所以我猜测它会对屏幕上所有导致延迟的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 code> 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天全站免登陆