Xamarin ListView不显示任何数据

问题描述 投票:0回答:2

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

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

<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" });
    }
}
c# xaml listview xamarin
2个回答
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
投票

真的你需要做的就是确保ObservableCollection有一个public getter。然后你可以设置BindingContext = this,你可以在UI上访问它。但与往常一样,有许多方法可以为这些小猫提供皮肤。

© www.soinside.com 2019 - 2024. All rights reserved.