我创建了从JSON文件获取数据的服务,现在尝试在页面上显示它们。
在使用中,变量model
获得正确的值,但在ViewModel中,由于StationId,StationName,Message,DateText等变量为空,因此页面上显然没有任何显示。
(目前,我出于测试目的设置了一些硬编码值)
型号
public class IrrigNetModel
{
public IrrigNetModelItem[] items { get; set; }
}
public class IrrigNetModelItem
{
public int ID { get; set; }
public string Message { get; set; }
public DateTime Date { get; set; }
public string DateText { get; set; }
public int StationId { get; set; }
public string StationName { get; set; }
public float StationLongitude { get; set; }
public float StationLatitude { get; set; }
public int ServiceId { get; set; }
public string ServiceName { get; set; }
}
服务
class IrrigNetService
{
public static async Task<IrrigNetModel> GetServices(string token, string lngCode)
{
IrrigNetModel model = new IrrigNetModel();
try
{
string URL = DataURL.BASE_URL + "agronetmobile/getlistnotifications?lngCode=" + lngCode;
string content = Newtonsoft.Json.JsonConvert.SerializeObject(model);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
//client.DefaultRequestHeaders.TryAddWithoutValidation("Culture", LocalData.Lang);
HttpResponseMessage result = await client.PostAsync(URL, new StringContent(content, Encoding.UTF8, "application/json"));
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
string resultContent = await result.Content.ReadAsStringAsync();
model = (IrrigNetModel)Newtonsoft.Json.JsonConvert.DeserializeObject(resultContent, typeof(IrrigNetModel));
}
else if (result.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
//
}
}
}
catch (Exception ex)
{
model = null;
}
return model;
}
}
ViewModel:
public ObservableCollection<IrrigNetModelItem> IrrigNetCollection { get; set; } = new ObservableCollection<IrrigNetModelItem>
{
new IrrigNetModelItem
{
StationId = 1,
StationName = "Krakatosia",
Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur scelerisque a lorem sit amet mattis.",
DateText = "21.07.2012."
}
};
public IrrigNetViewModel()
{
var token = LocalData.Token;
IrrigNetService.GetServices(token,"sr");
TabTappedCommand = new Command((tabName) => OnTapClicked(tabName.ToString()));
HideListOnTapCommand = new Command(HideListOnTap);
IrrigNetModelItem model = new IrrigNetModelItem();
var irrigNetModel = new IrrigNetModelItem
{
StationId = model.StationId,
StationName = model.StationName,
Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur scelerisque a lorem sit amet mattis.",
DateText = model.DateText
};
IrrigNetCollection.Add(irrigNetModel);
}
答案 0 :(得分:0)
您正在致电服务但未存储结果
4
代替,试试这个
IrrigNetService.GetServices(token,"sr");