Xamarin ListView不显示任何数据

时间:2016-06-08 08:47:58

标签: c# xaml listview xamarin

在将数据列表绑定到XAML中的ListView时,我有一个问题,即我试图在我的应用中的ListView内显示数据。 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个项目。 / p>

问题是以下两个绑定中没有数据:

<TextCell Text="{Binding Name}" Detail="{Binding Surname}"/>

有人可以告诉或告诉我在绑定中我做错了什么吗?或者,如果您需要任何更多信息或代码,请询问!我很乐意提供更多信息。 :)

修改

我已更新了RandomList类并实现了INotifyPropertyChanged,但我仍无法通过XAML绑定将我的数据加载到ListView中。

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));
        }
    }
}

编辑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" });
    }
}

2 个答案:

答案 0 :(得分:1)

创建一个模型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));
    }
}

然后在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" });  

答案 1 :(得分:1)

您实际上要做的就是确保ObservableCollection有一个public getter。然后,您可以只设置BindingContext = this并可以在UI上访问它。但是像往常一样,有很多方法可以给这些小猫皮。