使用 While 循环在 Xamarin Forms 中填充自定义 ListView

3

我还是一个新手程序员,经过一天的思考后,我需要帮助。

最近我一直在使用Xamarin Forms中的列表视图,现在我想尝试使用while循环来生成所有行。

我真正想做的是使用while循环或其他方法来填充下面代码片段中花括号"{ }"内的区域。

 TransactionsList.ItemsSource = new List<Contacts>() {  };

目前它的设置方式是每当我添加

标签时,它会自动将其转换为

的形式。
new Contacts() { ListAmount = TransactionsAmounts[i], ListDescription = TransactionsDescriptions[i], } 

在括号内,我可以在列表视图中创建新行。这很棒,但现在我想以一种让我能够使用数组填充它而不必编写每一行以创建行的方式来做到这一点。我希望我的数组决定列表的大小。(数组的大小始终相同)
有没有更简单的方法?或者有没有一种使用循环之类的东西来填充花括号区域的方法?
希望这样描述有意义。我不太擅长像这样描述事情。如果您需要更多信息或其他内容,请告诉我。
下面是列表视图xaml:
<ListView x:Name="TransactionsList" HasUnevenRows="True">
        <ListView.ItemTemplate >
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal">
                        <StackLayout Orientation="Vertical">
                            <Label Text="{Binding ListAmount}" Font="18"></Label>
                            <Label Text="{Binding ListDescription}" TextColor="Gray"></Label>
                        </StackLayout>
                      <!--  <Label Text="{Binding ListDates}" TextColor="Blue" HorizontalOptions="EndAndExpand" HorizontalTextAlignment="End"></Label> -->
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

1个回答

3
var contacts = new List<Contacts>();

for (int ndx = 0; ndx < trasactionCount; ndx++) {
  contacts.Add(new Contacts { ListAmount = TransactionsAmounts[ndx], ListDescription = TransactionsDescriptions[ndx] });
}

TransactionsList.ItemsSource = contacts;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接