Xamarin ListView不显示任何数据 [英] Xamarin ListView not displaying any data

查看:60
本文介绍了Xamarin ListView不显示任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将数据列表绑定到XAML中的ListView时,我试图在应用程序的 ListView 中显示数据时遇到问题.ListView根本不填充任何数据.

I have a problem where I'm trying to display data inside a ListView in my app when binding a list of data to my ListView in XAML. The ListView does not populate with any data at all.

这是我所有的编码:

我的 代码隐藏

My Code-behind

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

My 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个项目.

When I insert a breakpoint in my code behind where I add the dummy data to the list, I can then see in my XAML here: <ListView x:Name="lwStudents" ItemsSource="{Binding randomList}"> that there are 4 items in my randomList.

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

The problem is that there is no data in the two bindings below:

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

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

Can someone please tell or show me what I'm doing wrong in my binding? Or if you need ANY more info or code, please ask! I will be happy to give more info. :)

编辑

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

I've updated my RandomList class and implemented INotifyPropertyChanged, but I still can't get my data to load in my ListView through XAML bindings.

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页面目前的样子:

This is how my MainUI.xaml.cs page looks like at the moment:

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

Create a model 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页面中,在初始化组件之后设置绑定上下文.

And then in your MainUI Page set the binding context just after initialize component.

BindingContext = new MainUIModel();

您Xaml中的绑定randomList将引用此新创建的模型,而您的Name(姓),Surname(名称)将引用该列表模型.两种绑定上下文是不同的.您可以使用以下代码添加数据.

The binding randomList in your Xaml will refer to this new created model, and your Name, Surname will refer to the list model. Both binding context are different. You can add your data by using the below code.

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

这篇关于Xamarin ListView不显示任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆