c# - Xamarin.Forms Collection View

标签 c# xamarin uicollectionview gallery xamarin.forms

在 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/

相关文章:

ios - 从 XIB 创建的 UICollectionViewCell 在拖放过程中会导致闪烁

c# - WPF .NET4.0 内容控件超出屏幕

c# - LINQ:为什么我的 IQueryable.Intersect 抛出异常

ios - 位置跟踪应用程序在几分钟后在后台暂停

swift - 单元格未出现在 UICollectionView 中?

ios - UICollectionView 不呈现所有行

c# - WPF 中的 ObservableCollection 绑定(bind)

c# - UWP - 获取下拉列表中的蓝牙设备列表

unit-testing - 我可以在 Visual Studio 单元测试(类库)项目中使用 Xamarin.Forms.DependencyService 吗?

Xamarin 多重 dex 问题