【发布时间】:2016-10-12 22:50:23
【问题描述】:
我正在尝试在 Xamarin 中创建绑定的 ListView。这是 C# 代码:
public partial class LearnPage : ContentPage
{
public class TodoItem
{
public string DisplayName { get; set; }
}
//ObservableCollection<TodoItem> Items = new ObservableCollection<TodoItem>();
private IEnumerable<TodoItem> _items;
public IEnumerable<TodoItem> Items
{
get { return _items; }
set
{
if (Equals(_items, value))
return;
_items = value;
OnPropertyChanged();
}
}
public LearnPage ()
{
InitializeComponent();
BindingContext = this;
Items = new TodoItem[]{
new TodoItem{ DisplayName = "Milk cartons are recyclable" }
};
//Items.Add(new TodoItem { DisplayName = "Milk cartons are recyclable" });
}
}
您还可以看到一些带有ObervableCollection 的注释掉的代码,我也尝试过。
这是 XAML:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="learn.LearnPage">
<ContentPage.Content>
<StackLayout VerticalOptions="FillAndExpand" Padding="0,10,0,10">
<ListView ItemsSource="{Binding Items}" RowHeight="40" x:Name="sarasas">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding DisplayName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
当我构建应用程序时,会显示一个空列表。我对 Xamarin 真的很陌生,我想我只是错过了一些明显的东西。任何帮助表示赞赏!
【问题讨论】:
-
这发生在我身上,我尝试分配给列表 n 不同的方式试试这个: var list = new TodoItem[]{ new TodoItem{ DisplayName = "Milk carton are recyclable" } };项目=列表;看看有没有帮助
-
感谢您的提示,但问题仍然存在。
标签: c# listview xamarin xamarin.forms