如何在我的水平StackLayout / Scrollview中正确地从我的数据库中收集图像列表+标签?

时间:2016-07-01 15:12:26

标签: c# xamarin xamarin.forms

我的布局问题有两个问题。 第一个问题是我正在尝试制作Image Aspect.AspectFill但是图像不会使用我当前的代码进行AspectFill(它们保持不同的大小)。

第二个问题是布局本身。

使用我当前的代码看起来是这样的:

 <  IMAGE -> IMAGENAME -> LABEL -> IMAGE -> IMAGENAME -> LABEL etc. >

我不希望显示IMAGENAME。

我想要实现的目标是,IMAGE与下面的右侧LABEL连接:

< IMAGE    IMAGE    IMAGE    IMAGE >
< LABEL    LABEL    LABEL    LABEL >

这是我正在使用的当前代码:

我的XAML:

<ScrollView Orientation="Horizontal" HorizontalOptions="StartAndExpand">
<StackLayout x:Name = "myStack" Orientation="Horizontal" 
HorizontalOptions="StartAndExpand">

</StackLayout>
</ScrollView>

这是我的代码:

public List <string> imageList = new List<string> ();

async void loadImages ()
{
    var getImages = await phpApi.getImages ();

    foreach (var theitems in getImages ["results"])
    {
        var value = "";
        var valueTwo = "";
        imageList.Add 
        (value = theitems ["Photo"].ToString());

        imageList.Add 
        (valueTwo = theitems ["PhotoName"].ToString());
    }

    for (int i = 0; i < imageList.Count; i++) {

        var image = new Image ();
            AbsoluteLayout.SetLayoutBounds (image, new Rectangle (0.5, 0.6, 0.5, 0.4)); 
            image.Source = imageList [i];
            image.Aspect = Aspect.AspectFill;

            var label = new Label ();
            AbsoluteLayout.SetLayoutBounds (label, new Rectangle (0.5, 1.0, 0.5, 0.4));
            label.Text = imageList [i];

            myStack.Children.Add (image);
            myStack.Children.Add (label);

    }

}

1 个答案:

答案 0 :(得分:1)

public Dictionary<string, string> imageList = new Dictionary<string,string> ();

async void loadImages ()
{
    var getImages = await phpApi.getImages ();

    foreach (var theitems in getImages ["results"])
    {
        imageList.Add(
          theitems ["Photo"].ToString(),
          theitems ["PhotoName"].ToString()
        );

    }

    foreach (var key in imageList.Keys) {

        // vertical stack for each image/label pair
        var inner = new StackLayout();

        var image = new Image ();
        image.Source = key;
        image.Aspect = Aspect.AspectFill;

        var label = new Label ();
        label.Text = imageList [key];

        inner.Children.Add(image);
        inner.Children.Add(label);
        myStack.Children.Add (inner);

    }

}