我是C#和XAML的新手,正在开发一个简单的天气应用程序,它可以让你搜索城市和州,并返回一个格式化的10天预报列表。
我在使用ListView显示从Wunderground Weather API检索的数据时遇到了问题。这是我正在使用的JSON示例。
我的问题:我如何通过XAML中的一个模板运行所有列表项,该模板在我们从Wunderground API获得结果后显示项的属性(icon_url,title,fcttext)?
下面是我的模型:
public class ForecastList { public class Forecastday { public string icon_url { get; set; } public string title { get; set; } public string fcttext { get; set; } } public class TxtForecast { public List forecastday { get; set; } } public class Forecast { public TxtForecast txt_forecast { get; set; } } public class RootObject { public Forecast forecast { get; set; } } }
XAML:
(lt)堆栈布局填充=30&>;堆栈布局方向=水平&>;(<entry horizontaloptions=fillandexpand“placeholder=city”x:name=city“/&>;(<entry占位符=2字母状态“x:name=state”/&>;lt;/stacklayout&>;<Button text=search“clicked=onclicked”/&>;(<ListView ItemsSource=绑定ListSource&>;(<ListView.ItemTemplate&>;数据模板(<);<Label Text=Binding Title}“/&>;<Label text=Binding FctText}“/&>;(<Image source=Binding IconUrl}“/&>;lt;/datatemplate&>;lt;/ListView.ItemTemplate&>;lt;/ListView&>;lt;/stacklayout&>;
和ViewModel:
public class MainPageViewModel { public async Task GetWeatherAsync(string url) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(url); var response = await client.GetAsync(client.BaseAddress); response.EnsureSuccessStatusCode(); var JsonResult = response.Content.ReadAsStringAsync().Result; var weather = JsonConvert.DeserializeObject(JsonResult); SetList(weather); } public List ListSource { get; set; } private string _title; public string Title { get { return _title; } set { _title = value; } } private string _fctText; public string FctText { get { return _fctText; } set { _fctText = value; } } private string _iconUrl; public string IconUrl { get { return _iconUrl; } set { _iconUrl = value; } } private void SetList(ForecastList.RootObject weather) { ListView listView = new ListView(); var forecastList = weather.forecast.txt_forecast.forecastday; List listSource = new List(forecastList); ListSource = listSource; listView.ItemsSource = ListSource; } }
干杯!
您需要使用DTO's将服务器数据转换为正常形式
例如:
这是我的Dto
public class SampleDto
{
public string Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
像这样从服务器获取数据
public static async Task<T> GetResultFromApi<T>(string serviceUrl)
{
try
{
GetConnection();
var response = await _httpClient.GetAsync(new Uri(yourUrl + serviceUrl));
var stringAsync = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
var responseJson = stringAsync;
return JsonConvert.DeserializeObject<T>(responseJson);
}
LoggingManager.Error("Received error response: " + stringAsync);
return default(T);
}
catch (Exception exception)
{
LoggingManager.Error(exception);
return default(T);
}
}
var gettingDto = await GetResultFromApi<SampleDto>(string.Format(client.BaseAddress));
最后将myDto转换为我想要的原始格式
var entity = ConvertGameDtoToEntity(gettingDto);
public SampleEntity ConvertGameDtoToEntity(SampleDto gettingDto)
{
return new SampleEntity
{
Id = gettingDto.Id,
Name= gettingDto.Name,
Address = gettingDto.Address,
};
}
以下链接有清晰的描述https://www.codeproject.com/articles/36781/serialization-and-deserialization-in-asp-net-with