如何修复Xamarin.Forms移动应用程序页面中的空列表视图



我正在学习使用Xamarin.forms with Prism,我想从列表中的每个对象中填充带有特定项目的列表视图。我的列表视图显示为空,但我看不到任何错误。

我尝试使用绑定来绑定列表中对象中的名称变量。这是空的,所以我尝试使用可观察的集合获取我的列表并添加每个名称。

这是动物对象模型

using System;
namespace LearningPrism.Models
{
    class Animal
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public decimal Happiness { get; set; }
        public void PrintBase()
        {
            Console.WriteLine($"Name: {Name}");
            Console.WriteLine($"Age: {Age}");
            Console.WriteLine($"Happy: {Happiness}");
            Console.WriteLine();
        }
    }
}

我使用品种类作为对象创建我的列表,并具有3个功能,这些功能获得了该类型的过滤列表(未经滤光而获得所有品种(。

namespace LearningPrism.Models
{
    class Breed
    {
        public static List<Animal> _breedList = new List<Animal>
            {
                new Animal
                {
                    Id = Guid.NewGuid().ToString(),
                    Name = "Greyhound",
                    Type = BreedType.Dog
                },
//There are 3 more Dog Animal breeds but I have removed them so it is easier to read
                new Animal
                {
                    Id = Guid.NewGuid().ToString(),
                    Name = "Bengal",
                    Type = BreedType.Cat
                }, 
//There are 3 more Cat Animal breeds but I have removed them so it is easier to read
                }
            };
        public static List<Animal> GetAllBreeds()
        {
            return _breedList;
        }
        public static List<Animal> GetBreedsByType(BreedType type)
        {
            switch (type)
            {
                case BreedType.Dog:
                    return (from Animal in _breedList where Animal.Type == BreedType.Dog select Animal).ToList();
                case BreedType.Cat:
                    return (from Animal in _breedList where Animal.Type == BreedType.Cat select Animal).ToList();
                default:
                    return _breedList;
            }
        }
    }
}

这是我的视图模型:

namespace LearningPrism.ViewModels
{
    public class PageAViewModel : ViewModelBase
    {
        private List<Breed> MyList { get; set; }
        public PageAViewModel(INavigationService navigationService) : base(navigationService)
        {
            Title = "Hello Human";
        }
        private void LoadData()
        {
            ObservableCollection<Animal> myData = new ObservableCollection<Animal>(Breed.GetAllBreeds() as List<Animal>);
        }
        public override void OnNavigatingTo(INavigationParameters parameters)
        {
            base.OnNavigatingTo(parameters);
            LoadData();
        }
    }
}

这是页面的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"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="LearningPrism.Views.PageA" Title="{Binding Title}">
    <Label Text="{Binding Title}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" />
    <StackLayout>
        <ListView HorizontalOptions="FillAndExpand" 
                  VerticalOptions="FillAndExpand" 
                  ItemsSource="{Binding myData}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackLayout HorizontalOptions="CenterAndExpand">
                        <Label Text="{Binding Breed.Name}" TextColor="Black"></Label>
                    </StackLayout>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

首先,要将您的ListView绑定到myDatamyData必须是公共属性。

public ObservableCollection<Animal> myData { get; set; }

如果列表中的每个项目都是Animal,则您的绑定路径将为Text="{Binding Name}",因为NameAnimal的属性。没有Breed属性。

如上所述

您的财产:

private List<Breed> MyList { get; set; }

应该是公开的。我还建议您使用Prism使用属性更改事件:将您的财产更改为:

private List<Breed> _myList;
    public List<Breed> MyList
    {
        get { return _myList; }
        set { _myList = value; RaiseOnPropertyChanged(); }
    }

然后将您的列表XAML更改为:

<ListView HorizontalOptions="FillAndExpand" 
              VerticalOptions="FillAndExpand" 
              ItemsSource="{Binding MyList}">
        <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                 <StackLayout HorizontalOptions="CenterAndExpand">
                     <Label Text="{Binding Name}" TextColor="Black"> 
                     </Label>                  
                </StackLayout>
              </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

最新更新