C# Xamarin表单列表视图不显示文本单元格
这是我正在使用的代码C# Xamarin表单列表视图不显示文本单元格,c#,listview,xamarin,xamarin.forms,C#,Listview,Xamarin,Xamarin.forms,这是我正在使用的代码 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, De
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))
}
}
}
};
items=新列表();
添加(新的TextCell{Text=“Cake”,TextColor=Color.Green,Detail=“还剩12小时!”,DetailColor=Color.Black});
添加(新的TextCell{Text=“Pie”,TextColor=Color.Green,Detail=“还剩14小时!”,DetailColor=Color.Black});
var buy=新内容页
{
Title=“购买”,
内容=新的堆栈布局
{
垂直选项=布局选项。中心,
儿童={
新列表视图
{
ItemsSource=项目,
ItemTemplate=新的数据模板(typeof(TextCell))
}
}
}
};
ListView由两个没有内容的空白视图填充
是否缺少某些属性?对于ListView,ItemsSource是包含数据的对象的集合,但它们本身不是视图/单元格/其他UI对象。它们是您的域数据 另一方面,ItemTemplate需要返回一个设置了正确绑定的单元格,因此当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;
})
}
}
}
};
公共类ThingToBuy
{
公共字符串What{get;set;}
公共字符串HowLong{get;set;}
}
项目=新列表();
添加(newthingtobuy{What=“Cake”,HowLong=“还剩12小时!”);
添加(newthingtobuy{What=“Pie”,HowLong=“还剩14小时!”);
var buy=新内容页
{
Title=“购买”,
内容=新的堆栈布局
{
垂直选项=布局选项。中心,
儿童={
新列表视图
{
ItemsSource=项目,
ItemTemplate=新数据模板(()=>{
var cell=新的TextCell();
cell.SetBinding(Label.TextProperty,“What”);
cell.SetBinding(Label.DetailProperty,“HowLong”);
返回单元;
})
}
}
}
};
有关ItemsSource/ItemTemplate的更详细示例,请参阅ListView文档: