Xamarin ListView不显示任何数据
我有一个问题,当我尝试在我的应用程序中显示ListView
中的数据时,将数据列表绑定到XAML中的ListView中。 ListView不会填充任何数据。Xamarin ListView不显示任何数据
这里是我的所有编码:
我代码隐藏
public ObservableCollection<RandomList> randomList = new ObservableCollection<RandomList>();
public App()
{
MainPage = new MainUI();
randomList.Add(new RandomList { Name = "Susan", Surname = "Potgieter", School = "Oosterlig" });
randomList.Add(new RandomList { Name = "Jonathan", Surname = "Visagie", School = "Elspark" });
randomList.Add(new RandomList { Name = "Gerhard", Surname = "Vermaak", School = "E.G. Jansen" });
randomList.Add(new RandomList { Name = "Kobus", Surname = "Jacobs", School = "Oosterlig" });
}
我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="RandomTest.MainUI">
<StackLayout Padding="0,40,0,0">
<Label Text="Hello" VerticalOptions="Center" HorizontalOptions="Center" />
<Button x:Name="btnGetStudents" Text="Get Students"/>
<ListView x:Name="lwStudents" ItemsSource="{Binding randomList}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" Detail="{Binding Surname}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
当我在我的代码中插入一个断点是后面我将虚拟数据添加到列表中,然后我可以在我的XAML中看到:<ListView x:Name="lwStudents" ItemsSource="{Binding randomList}">
在我的randomList
中有4个项目。
的问题是,有如下两个绑定没有数据:
<TextCell Text="{Binding Name}" Detail="{Binding Surname}"/>
有人能告诉或告诉我什么,我做错了,我结合?或者,如果您需要更多信息或代码,请询问!我会很乐意提供更多信息。 :)
编辑
我已经更新了我RandomList
类和实现INotifyPropertyChanged
,但我还是不能让我的数据在我的ListView通过XAML绑定加载。
using System.ComponentModel;
namespace RandomTest
{
public class RandomList : INotifyPropertyChanged
{
public int Id { get; set; }
string _name;
public string Name
{
get { return _name; }
set
{
if (_name != value)
_name = value;
OnPropertyChanged("Name");
}
}
string _surname;
public string Surname
{
get { return _surname; }
set
{
if (_surname != value)
_surname = value;
OnPropertyChanged("Surname");
}
}
string _school;
public string School
{
get { return _school; }
set
{
if (_school != value)
_school = value;
OnPropertyChanged("School");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
EDIT 2
这是我MainUI.xaml.cs页面的外观像此刻:
public partial class MainUI : ContentPage
{
public MainUI()
{
InitializeComponent();
BindingContext = new MainUIModel();
MainUIModel mainUIModel = (MainUIModel)this.BindingContext;
mainUIModel.randomList.Add(new RandomList { Name = "Susan", Surname = "Potgieter", School = "Oosterlig" });
mainUIModel.randomList.Add(new RandomList { Name = "Jonathan", Surname = "Visagie", School = "Elspark" });
mainUIModel.randomList.Add(new RandomList { Name = "Gerhard", Surname = "Vermaak", School = "E.G. Jansen" });
mainUIModel.randomList.Add(new RandomList { Name = "Kobus", Surname = "Jacobs", School = "Oosterlig" });
}
}
创建一个模型MainUIModel
public class MainUIModel : INotifyPropertyChanged
{
ObservableCollection<RandomList> _randomList;
public ObservableCollection<RandomList> randomList
{
get { return _randomList; }
set
{
if (_randomList != value)
_randomList = value;
OnPropertyChanged("randomList");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
而且那么在初始化compo之后,在你的MainUI页面中设置绑定上下文新界东北堆填区。
BindingContext = new MainUIModel();
Xaml中的绑定randomList将引用这个新创建的模型,并且您的Name,Surname将引用列表模型。两个绑定上下文都不同。您可以使用下面的代码添加您的数据。
MainUIModel mainUIModel = (MainUIModel)this.BindingContext;
mainUIModel.randomList=new ObservableCollection<RandomList>();
mainUIModel.randomList.Add(new RandomList { Name = "Susan", Surname = "Potgieter", School = "Oosterlig" });
mainUIModel.randomList.Add(new RandomList { Name = "Jonathan", Surname = "Visagie", School = "Elspark" });
mainUIModel.randomList.Add(new RandomList { Name = "Gerhard", Surname = "Vermaak", School = "E.G. Jansen" });
mainUIModel.randomList.Add(new RandomList { Name = "Kobus", Surname = "Jacobs", School = "Oosterlig" });
感谢您的回答阿卡什。我做了像你所建议的,但我得到错误'System.NullReferenceException:对象引用未设置为对象的实例'在行:'MainUIModel mainUIModel =(MainUIModel)this.BindingContext;'。 – CareTaker22
你把这条线放在哪里?它必须在你的MainUI页面中,因为bindingcontext将在MainUI页面上设置,并且'this'也指向你的MainUI页面。 –
在我的公共类App:Application窗口中,我将虚拟数据添加到列表中。 – CareTaker22
您是否在您的模型中继承了'INotifyPropertyChanged',您可能已将其设置为'BindingContext'? –
你好! :)不,我没有'INotifyPropertyChanged'类。你认为它可以帮助吗?这很奇怪,因为我可以在代码中设置listview的itemsource并显示所有数据,但它在XAML中不起作用...:| – CareTaker22
是的,在你的模型中需要。@ CareTaker22 https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_bindings_to_mvvm/ –