gpt4 book ai didi

c# - Xamarin.Forms Collection View

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:06 24 4
gpt4 key购买 nike

在 Xamarin.Forms 中重新创建类似 iOS 的 UICollectionView 的 View 的最佳方法是什么?我需要它是跨平台的。我立即想到的两个选项是使用 Xamarin.Forms.Grid 和 Xamarin.Forms.Listview(将单元格自定义为具有 3 个“列”)。还有其他想法或意见吗?顺便说一句,这将用于图片库。

谢谢

最佳答案

您可以使用以下代码来使用 RelativeLayout 使其表现得像 StackPanel

public class MyContentPage : ContentPage
{
public MyContentPage()
{
var layout = new RelativeLayout();

var box1 = new ContentView
{
BackgroundColor = Color.Gray,
Content = new Label
{
Text = "0"
}
};

double padding = 10;
layout.Children.Add( box1, () => new Rectangle(((layout.Width + padding) % 60) / 2, padding, 50, 50));

var last = box1;
for (int i = 0; i < 200; i++)
{
var relativeTo = last; // local copy
var box = new ContentView
{
BackgroundColor = Color.Gray,
Content = new Label
{
Text = (i + 1).ToString()
}
};

Func<View, bool> pastBounds = view => relativeTo.Bounds.Right + padding + view.Width > layout.Width;
layout.Children.Add(box, () => new Rectangle(pastBounds(relativeTo) ? box1.X : relativeTo.Bounds.Right + padding,
pastBounds(relativeTo) ? relativeTo.Bounds.Bottom + padding : relativeTo.Y,
relativeTo.Width,
relativeTo.Height));

last = box;
}

Content = new ScrollView { Content = layout, Padding = new Thickness(6) };
}
}

关于c# - Xamarin.Forms Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25387316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com