簡體   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