我正在学习将Xamarin.Forms与棱镜一起使用,我想用列表中每个对象的特定项目填充列表视图。我的列表视图显示为空,但没有看到任何错误。
我尝试使用绑定绑定到列表中对象中的Name变量。那显示为空,所以我尝试使用Observable Collection获取列表并添加每个名称。
这是动物对象模型
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();
}
}
}
我使用Breed类作为对象创建列表,并且具有以下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>
答案 0 :(得分:2)
首先,要将您的ListView
绑定到myData
,myData
必须是公共财产。
public ObservableCollection<Animal> myData { get; set; }
如果列表中的每个项目都是Animal
,那么您的绑定路径将是Text="{Binding Name}"
,因为Name
是Animal
的属性。没有Breed
属性。
答案 1 :(得分:1)
如Jason所述
您的财产:
private List<Breed> MyList { get; set; }
应该是公开的。我还建议您是否使用棱镜来使用属性更改事件:将属性更改为:
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>