gpt4 book ai didi

c# - 如何修复 xamarin.forms 移动应用程序页面中的空 ListView

转载 作者:太空宇宙 更新时间:2023-11-03 22:34:10 24 4
gpt4 key购买 nike

我正在学习将 Xamarin.Forms 与 prism 结合使用,我想用列表中每个对象的特定项目填充 ListView 。我的 ListView 显示为空,但我没有看到任何错误。

我已经尝试使用绑定(bind)来绑定(bind)到我的对象中的 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;

}
}
}
}

这是我的 View 模型:

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 绑定(bind)到 myDatamyData 必须是公共(public)属性。

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

如果列表中的每一项都是Animal,那么您的绑定(bind)路径将为Text="{Binding Name}",因为NameAnimal 的属性。没有 Breed 属性。

关于c# - 如何修复 xamarin.forms 移动应用程序页面中的空 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com