xamarin.forms - Xamarin 表单手势拖拽闪烁
问题描述
我想要的是创建一个可以在屏幕上拖动的框,所以我创建了一个扩展类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);
}
}
问题是当我开始拖动框时,它具有闪烁的效果,如下图所示。如何解决?还是有其他方法可以创建可拖动元素?
解决方案
我使用你的代码,但我没有和你一样的问题,所以我建议你可以在 OnPanUpdated 事件中尝试以下代码。
private double _xOffset, _yOffset;
private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
switch (e.StatusType)
{
case GestureStatus.Started:
Debug.WriteLine("DEBUG: start dragging");
break;
case GestureStatus.Running:
this.TranslationX = _xOffset + e.TotalX;
this.TranslationY = _yOffset + e.TotalY;
break;
case GestureStatus.Completed:
_xOffset = this.TranslationX;
_yOffset = this.TranslationY;
Debug.WriteLine("DEBUG: drag ended");
break;
}
}
更新:
我认为您在 PanContainer 中可能有一些问题,请修改您的代码如下:
public class PanContainer : ContentView
{
private double x, y;
public PanContainer()
{
var panGesture = new PanGestureRecognizer();
panGesture.PanUpdated += OnPanUpdated;
this.GestureRecognizers.Add(panGesture);
}
private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
switch (e.StatusType)
{
case GestureStatus.Started:
Debug.WriteLine("DEBUG: start dragging");
break;
case GestureStatus.Running:
Content.TranslationX = x + e.TotalX;
Content.TranslationY = y + e.TotalY;
break;
case GestureStatus.Completed:
x = Content.TranslationX;
y = Content.TranslationY;
Debug.WriteLine("DEBUG: drag ended");
break;
}
}
}
然后 ContentPage.cs:
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
Label label = new Label() { Text = "Hello world" ,BackgroundColor=Color.Green, WidthRequest=100,HeightRequest=150};
PanContainer box = new PanContainer();
box.Content = label;
container.Children.Add(box);
}
}
PanContainer 不能拖放,只能移动 PanContainer 的内容,如下所示:
推荐阅读
- flutter - 使用flutter(dart)更新具有特殊字符的firestore字段
- c# - 在一个 Apache Ignite 节点中支持多个环境
- java - 如何使用 Spring Boot-Web 应用程序将对象放入 aws s3 存储桶
- python - 如何从列表中删除不需要的列表括号?
- c# - CreatedAtAction 导致“没有路由与提供的值匹配”
- unity3d - 团结 | 更新还是调用重复?
- c# - 从一个函数到另一个函数的输出,特别是一个数组
- c++ - 在 qml 中对枚举类使用 Q_ENUM 和 Q_PROPERTY
- azure-data-lake-gen2 - Appnexus 到 Azure 数据迁移错误(使用 sas)
- html - 我可以从中心填充网格容器吗?