gpt4 book ai didi

c# - Xamarin Forms ListView 不显示 TextCells

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:13 25 4
gpt4 key购买 nike

这是我现在正在使用的代码...

items = new List<TextCell>();
items.Add(new TextCell { Text = "Cake", TextColor = Color.Green, Detail = "12 hours left!", DetailColor = Color.Black});
items.Add(new TextCell { Text = "Pie", TextColor = Color.Green, Detail = "14 hours left!", DetailColor = Color.Black });


var buy = new ContentPage
{
Title = "Buy",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new ListView
{
ItemsSource = items,
ItemTemplate = new DataTemplate(typeof(TextCell))
}
}
}
};

ListView 填充了两个没有内容的空白 View 。

我是否缺少某些属性?

最佳答案

对于 ListView,ItemsSource 是包含数据的对象集合,但它们本身不是 Views/Cells/其他 UI 对象。它们是您的域数据。

另一方面,ItemTemplate 需要返回一个设置了正确绑定(bind)的 Cell,因此当 ListView 将其 BindingContext 设置为来自 ItemsSource 的对象时,所有字段都已设置。

对于您的情况,它可能看起来像:

public class ThingToBuy
{
public string What { get; set; }
public string HowLong { get; set; }
}

items = new List<ThingToBuy>();
items.Add(new ThingToBuy { What = "Cake", HowLong = "12 hours left!" });
items.Add(new ThingToBuy { What = "Pie", HowLong = "14 hours left!" });

var buy = new ContentPage
{
Title = "Buy",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new ListView
{
ItemsSource = items,
ItemTemplate = new DataTemplate(() => {
var cell = new TextCell();
cell.SetBinding(Label.TextProperty, "What");
cell.SetBinding(Label.DetailProperty, "HowLong");
return cell;
})
}
}
}
};

有关 ItemsSource/ItemTemplate 的更多详细示例,请参阅 ListView 的文档:https://developer.xamarin.com/api/type/Xamarin.Forms.ListView/

关于c# - Xamarin Forms ListView 不显示 TextCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40454771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com