【发布时间】:2019-12-10 13:24:44
【问题描述】:
我想要创建一个可以在屏幕上拖动的框,所以我创建了一个扩展 Frame 的类(但它也可以是 AbsoluteLayout,而不是 BoxView,因为我需要编写那个盒子里的东西)。
类拖动框
using System;
using System.Diagnostics;
using Xamarin.Forms;
namespace PanGesture
{
public class DragBox : Frame
{
public DragBox()
{
var panGesture = new PanGestureRecognizer();
this.GestureRecognizers.Add(panGesture);
panGesture.PanUpdated += OnPanUpdated;
BorderColor = Color.Blue;
Padding = 4;
BackgroundColor = Color.Green;
Content = new Label
{
Text = "Word",
};
}
private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
switch (e.StatusType)
{
case GestureStatus.Started:
Debug.WriteLine("DEBUG: start dragging");
break;
case GestureStatus.Running:
this.TranslationX = e.TotalX;
this.TranslationY = e.TotalY;
break;
case GestureStatus.Completed:
Debug.WriteLine("DEBUG: drag ended");
break;
}
}
}
}
在主页中我只是创建了一个 DragBox 的实例
(容器是对主页上AbsoluteLayout的引用
public partial class HomePage : ContentPage
{
public HomePage ()
{
InitializeComponent ();
DragBox box = new DragBox();
AbsoluteLayout.SetLayoutBounds(box, new Rectangle(0.1, 0.1, 0.2, 0.2));
AbsoluteLayout.SetLayoutFlags(box, AbsoluteLayoutFlags.All);
container.Children.Add(box);
}
}
问题是,当我开始拖动框时,它具有闪烁的效果,如下图所示。 如何解决?或者有没有其他方法可以创建可拖动元素?
【问题讨论】:
-
你试过真机吗?这可能只是一个模拟器。
-
@AndresCastro 是的,它做同样的事情
标签: xamarin.forms drag-and-drop frame draggable uipangesturerecognizer