如何为从db收集的每个标签创建一个新索引(我的CarouselPage中的ContentPage)?

时间:2016-05-02 08:03:50

标签: c# xamarin xamarin.forms

这是我的代码。我想实现所以我为每个从db收集的标签创建一个新的carouselpage。所以我创建了一个for int,我从标签中获取了值,但现在当我想更进一步为每个标签制作一个carouselpage时,我不知道如何让它工作。

我的页面是代码和xaml的carouselpage:所以

public partial class PicturePage : CarouselPage

和xaml:

<CarouselPage> &&   </CarouselPage>

我的代码:

List<string> labelList = new List<string> ();

async   void loadtest ()
    {
        var getItems = await parseAPI.getInformation ();
        foreach (var items in getItems ["results"])
        {
            var value = "";
            imagesList.Add (value = items ["label"].ToString());
        }

        base.OnCurrentPageChanged ();
        var index = Children.IndexOf (CurrentPage);


        for (int i = 0; i < labelList.Count; i++ && i < index; i++ ) { //how should I add a new index for each label I gather?

            myLabel.Text = myLabel.Text + labelList [i]+ "\n";

        }


    }

1 个答案:

答案 0 :(得分:1)

通过设置Children属性并在运行时动态创建ContentPages来添加它们:

for (int i = 0; i < imagesList.Count; i++){
    var carouselChild = new ContentPage {
            Padding = padding,
            Content = new StackLayout {
                Children = {
                    new Label {
                        Text = labelList[i],
                        FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
                        HorizontalOptions = LayoutOptions.Center
                    }, 
                    new Image { Source = imagesList[i] }
                }
            }
    };

    Children.Add (carouselChild);
}