C# 放置列表<;T>;在ListView中
我有一个模型:C# 放置列表<;T>;在ListView中,c#,.net,wpf,C#,.net,Wpf,我有一个模型: public class BandModel { public string BandName { get; set; } public CountryModel Country { get; set; } public GenreModel Genre{ get; set; } public DateTime DateOfBirth { get; set; } } 有一个类返回这些类的列表 public class FullBandProces
public class BandModel
{
public string BandName { get; set; }
public CountryModel Country { get; set; }
public GenreModel Genre{ get; set; }
public DateTime DateOfBirth { get; set; }
}
有一个类返回这些类的列表
public class FullBandProcessor
{
public static async Task<List<BandModel>> LoadFullBandInformation()
{
string url = "http://localhost:11727/api/Bands";
using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
List<BandModel> result = await response.Content.ReadAsAsync<List<BandModel>>();
return result;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
}
LoadFullBandInformation
返回列表
,而\u BandModels
是一个可观察的集合
您应该替换:
get
{
return _BandModels = FullBandProcessor.LoadFullBandInformation();
}
与
get
{
return _BandModels=新的ObservableCollection(FullBandProcessor.LoadFullBandInformation());
}
您需要使用数据绑定和项目模板来实现这一点。另外,您实际上需要等待对FullBandProcessor.LoadFullBandInformation()的调用代码>某处。以下是更改为工作的C#代码:
公共类您的类
{
public ObservableCollection BandModels{get;private set;}=new ObservableCollection()//将其初始化为空
//建造师
公共课
{
SetBandModels();
}
专用异步任务SetBandModels()
{
//添加一个try/catch块
var bands=await FullBandProcessor.LoadFullBandInformation().ConfigureAwait(false);
波段模型=新的可观测集合(波段);
}
}
然后在XAML中,创建一个
及其项模板和绑定。像这样:
//添加更多具有更多绑定的控件
调用LoadFullBandInformation()
时,可能缺少await
。
不,await没有帮助您在此处编写的代码甚至没有编译,因此我们如何调试您看到的不同问题?LoadFullBandInformation
需要await
,这不会编译。BandModels=新的ObservableCollection(bands);BandModels显示错误:“无法将属性或索引器'YourClass.BandModels'分配给--它是只读的”。哇,抱歉,更新了代码并添加了专用集将代码>添加到属性描述
get
{
return _BandModels = FullBandProcessor.LoadFullBandInformation();
}
get
{
return _BandModels = new ObservableCollection<BandModel> (FullBandProcessor.LoadFullBandInformation());
}