繁体   English   中英

Xamarin.Forms集合视图

[英]Xamarin.Forms Collection View

在Xamarin.Forms中重新创建像iOS的UICollectionView这样的视图的最佳方法是什么? 我需要它是跨平台的。 我马上想到的两个选项是使用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) };
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM