Xamarin.Forms 可重新排序的 WrapLayout

Xamarin.Forms Reorderable WrapLayout

我有一组单元格(StackLayout 类型),它们都在 WrapLayout. I can't seem to figure out how to make it reorderable, similar to Drag & Drop ListView

这是 WrapLayout:

WrapLayout layout = new WrapLayout
{
    Spacing = 5,
    Orientation = StackOrientation.Horizontal,
    Padding = new Thickness(5, Device.OnPlatform(20, 5, 5), 5, 5),
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand,
};

这是一个单元格:

// Configure cell
var cell = new StackLayout
{
    WidthRequest = Device.Idiom == TargetIdiom.Phone ? 60 : 140,
    HeightRequest = Device.Idiom == TargetIdiom.Phone ? 60 : 50,
    BackgroundColor = Color.FromRgb(60, 56, 72),
    Children = {
        new Label {
            Text = Device.Idiom == TargetIdiom.Phone ? book.Contains("1") || book.Contains("2") || book.Contains("3") ? book.Substring(0,4).Replace(" ", "") : book.Substring(0,2) : book,
            FontSize = 18,
            TextColor = Color.White,
            LineBreakMode = LineBreakMode.TailTruncation,
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalTextAlignment = TextAlignment.Center,
            HorizontalTextAlignment = TextAlignment.Center
        }
    }
};
layout.Children.Add(cell);

下面是该应用程序目前的实际外观:

感谢任何帮助实现这一目标的人。

好吧,您所指的 WrapLayout 是一种自定义布局,我相信它是由开发人员创建的其中一个演示。要具有拖放功能,您必须自己手动实现它。

就我个人而言,我会将事件处理程序附加到单元格的 Tapped 事件:

var cell = new StackLayout
{
    Tapped += Cell_Tapped;
    WidthRequest = Device.Idiom == TargetIdiom.Phone ? 60 : 140,
    HeightRequest = Device.Idiom == TargetIdiom.Phone ? 60 : 50,
    BackgroundColor = Color.FromRgb(60, 56, 72),
    Children = {
        new Label {
            Text = Device.Idiom == TargetIdiom.Phone ? book.Contains("1") || book.Contains("2") || book.Contains("3") ? book.Substring(0,4).Replace(" ", "") : book.Substring(0,2) : book,
            FontSize = 18,
            TextColor = Color.White,
            LineBreakMode = LineBreakMode.TailTruncation,
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalTextAlignment = TextAlignment.Center,
            HorizontalTextAlignment = TextAlignment.Center
        }
    }
};
layout.Children.Add(cell);

然后创建事件处理程序:

void Cell_Tapped(object sender, EventArgs e)
{
    // Stuff you want to do with that cell
    // Make the current selected cell invisible, attach it to a boxview that is being moved by the tap event
}

然而,这仅适用于点击而不适用于真正的拖放实现,并且 Xamarin.Forms 目前无法让您访问触摸事件。您可以为 iOS 和 Android 上的单元格编写自定义呈现器,这将允许您访问两个平台上的 TouchesBegan 和 TouchesEnded 类型事件。

Xamarin link 给出了在 iOS 和 Android 上集成触摸的演练。

我希望这对您有所帮助,我确信对此有不同的实现。