C#&;XAML-从Wunderground API在ListView中显示JSON

C#&;XAML-从Wunderground API在ListView中显示JSON,c#,xaml,listview,xamarin.forms,wunderground,C#,Xaml,Listview,Xamarin.forms,Wunderground,我是C#和XAML的新手,正在开发一个简单的天气应用程序,可以让你搜索城市和州,并返回格式化的10天预报列表 我在使用ListView显示从中检索的数据时遇到问题。这是我正在使用的JSON 我的问题: 在我们从Wunderground API获得结果后,如何通过XAML中显示项目属性(icon_url,title,fcttext)的模板运行所有列表项目 这是我的模型: public class ForecastList { public class For

我是C#和XAML的新手,正在开发一个简单的天气应用程序,可以让你搜索城市和州,并返回格式化的10天预报列表

我在使用ListView显示从中检索的数据时遇到问题。这是我正在使用的JSON

我的问题: 在我们从Wunderground API获得结果后,如何通过XAML中显示项目属性(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; } } } 公共类预报员名单 { 公共舱位预报日 { 公共字符串图标\u url{get;set;} 公共字符串标题{get;set;} 公共字符串fcttext{get;set;} } 公共类TXT预测 { 公共列表预测日{get;set;} } 公共类预测 { 公共txtfecast txt_forecast{get;set;} } 公共类根对象 { 公共预测{get;set;} } } XAML:


以及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; } } 公共类MainPageViewModel { 公共异步任务GetWeatherAsync(字符串url) { HttpClient=新的HttpClient(); client.BaseAddress=新Uri(url); var response=await client.GetAsync(client.BaseAddress); response.EnsureSuccessStatusCode(); var JsonResult=response.Content.ReadAsStringAsync().Result; var weather=JsonConvert.DeserializeObject(JsonResult); 设置列表(天气); } 公共列表ListSource{get;set;} 私有字符串\u标题; 公共字符串标题 { 得到 { 返回标题; } 设置 { _标题=价值; } } 私有字符串_fctText; 公共字符串FctText { 得到 { 返回_fctText; } 设置 { _fctText=值; } } 私有字符串_iconUrl; 公共字符串图标 { 得到 { 返回iconUrl; } 设置 { _iconUrl=值; } } 私有void集合列表(ForecastList.RootObject weather) { ListView ListView=新建ListView(); var forecastList=weather.forecast.txt\u forecast.forecastday; List listSource=新列表(预测列表); ListSource=ListSource; listView.ItemsSource=ListSource; } }
干杯

您需要使用Dto将服务器数据转换为正常格式

例如:

这是我的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));
下面的链接有清晰的描述

var entity = ConvertGameDtoToEntity(gettingDto);

public SampleEntity ConvertGameDtoToEntity(SampleDto gettingDto)
{
        return new SampleEntity 
        {
            Id = gettingDto.Id,
            Name= gettingDto.Name,
            Address = gettingDto.Address,
        };
}