我在这里遇到一个小问题。代码看起来没问题,但Application没有绑定我在WebAPI数据库中插入的正确列表。
我期望实现的目标:
跨平台的Listview,在这种情况下,我在Android中测试,它列出了WebAPI代码中的所有数据,如下所示:
这是WebAPI上列出的Json文件
[{"Id":1,"BusinessName":"La ventana","BusinessFantasyName":"La Ventana","BusinessArea":"Restaurante","BusinessSpecificArea":"Pizzaria","Country":"Brasil","State":"RS","City":"Rio Grande","Adress":"Av Rio Grande","Neighbourhood":"Cassino","Number":"134","Complement":"Casa","Latitude":-32.1853837,"Longitude":-52.1582284,"DateOfReg":"26/12/2017","DateOfMod":"26/12/2017","BusinessStatus":"Ativo","RegisteredBy":"Daniel Serrão"},{"Id":2,"BusinessName":"Supermercado Granabara - Cassino","BusinessFantasyName":"Supermercado Granabara - Cassino","BusinessArea":"Supermercados","BusinessSpecificArea":"Bens e Consumo","Country":"Brasil","State":"RS","City":"Rio Grande","Adress":"Av Rio Grande","Neighbourhood":"Cassino","Number":"150","Complement":"NA","Latitude":-32.1845922,"Longitude":-52.1591549,"DateOfReg":"26/12/2017","DateOfMod":"26/12/2017","BusinessStatus":"Ativo","RegisteredBy":"Daniel Serrão"}]
以下是咨询此WEBAPI的代码:
namespace Icquire.RestClient
{ public class RestClient<T>
{
private const string WebServiceUrl = "http://localhost:58173/api/BusinessRegs/";
public async Task<List<T>> GetAsync()
{
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync(string.Format(WebServiceUrl));
var BusinessReg = JsonConvert.DeserializeObject<List<T>>(json);
return BusinessReg;
}
public async Task<bool> PostAsync(T t)
{
var httpClient = new HttpClient();
var json = JsonConvert.SerializeObject(t);
HttpContent httpContent = new StringContent(json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = await httpClient.PostAsync(WebServiceUrl, httpContent);
return result.IsSuccessStatusCode;
}
public async Task<bool> PutAsync(int Id, T t)
{
var httpClient = new HttpClient();
var json = JsonConvert.SerializeObject(t);
HttpContent httpContent = new StringContent(json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = await httpClient.PostAsync(WebServiceUrl + Id, httpContent);
return result.IsSuccessStatusCode;
}
public async Task<bool> DeleteAsync(int Id, T t)
{
var httpClient = new HttpClient();
var response = await httpClient.DeleteAsync(WebServiceUrl + Id);
return response.IsSuccessStatusCode;
}
}}
以下是内部服务的代码:
namespace Icquire.Services
{ public class BusinessRegServices
{ public async Task<List<BusinessReg>> GetBusinessRegAsync()
{
RestClient<BusinessReg> restClient = new RestClient<BusinessReg>();
var BusinessRegList = await restClient.GetAsync();
return BusinessRegList;
}
}
}
以下是查看数据的代码:
namespace Icquire.ViewModel
{ public class BusinessRegView : INotifyPropertyChanged
{
private List<BusinessReg> _BusinessRegList
{
get { return _BusinessRegList; }
set
{
_BusinessRegList = value;
OnPropertyChanged();
}
}
public List<BusinessReg> BusinessRegList { get; set; }
public BusinessRegView()
{
InitializeDataAsync();
}
private async Task InitializeDataAsync()
{
var businessRegServices = new BusinessRegServices();
BusinessRegList = await businessRegServices.GetBusinessRegAsync();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
};
}
这是在contentPage中的My ListView:
<ListView ItemsSource="{Binding BusinessRegList}" HasUnevenRows="True" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation ="Vertical">
<Label TextColor="Silver" Text="{Binding BusinessName}"/>
<Label TextColor="Silver" Text="{Binding Adress}"/>
<Label TextColor="Silver" Text="{Binding Number}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
问题在于,使用上面的代码,ListView中的Binding返回空白。由于Json正在正确生成,我认为问题出在咨询到Json文件上。或者在服务代码中。有没有人有线索?
提前致谢... 丹尼尔。
答案 0 :(得分:0)
假设您的服务调用实际上正在返回数据并且绑定设置正确。
您的财产声明不正确,应该是:
private List<BusinessReg> _BusinessRegList;
public List<BusinessReg> BusinessRegList
{
get { return _BusinessRegList; }
set
{
_BusinessRegList = value;
OnPropertyChanged();
}
}
更多信息可在Properties (C# Programming Guide)中找到。