【问题标题】:Xamarin.Forms Collection ViewXamarin.Forms 集合视图
【发布时间】:2014-08-19 15:27:04
【问题描述】:

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

谢谢

【问题讨论】:

    标签: c# xamarin uicollectionview gallery xamarin.forms


    【解决方案1】:

    您可以使用以下代码来使用 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) };
        }
    }
    

    【讨论】:

    • 这是一个聪明的解决方案,但它如何处理单元的回收/虚拟化?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多