【发布时间】:2017-12-01 08:22:01
【问题描述】:
我不是经验丰富的开发人员。我只是编程的菜鸟。
我已经设法通过这个网址http://samples.openweathermap.org/data/2.5/weather?lat=7.0744&lon=79.8919&appid=a7cae8ecfab2535dec05a83525f5ac7a 将当前天气信息发送到我的应用程序中
但我不知道如何从这个 openweathermap url http://samples.openweathermap.org/data/2.5/forecast?lat=7.0744&lon=79.8919&appid=a7cae8ecfab2535dec05a83525f5ac7a 获取 5 天天气预报数据
这是我创建的 Weatherforecast 类
class WeatherForecast
{
public async static Task<RootObject> GetWeatherForecast(double lat,double lon)
{
var httpn = new HttpClient();
var uri = String.Format("http://samples.openweathermap.org/data/2.5/forecast?lat={0}&lon={1}&appid=a7cae8ecfab2535dec05a83525f5ac7a", lat, lon);
var response = await httpn.GetAsync(uri);
var result = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<RootObject>(result);
return data;
}
}
public class Main
{
public double temp { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
public double pressure { get; set; }
public double sea_level { get; set; }
public double grnd_level { get; set; }
public int humidity { get; set; }
public int temp_kf { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Wind
{
public double speed { get; set; }
public double deg { get; set; }
}
public class Rain
{
}
public class Sys
{
public string pod { get; set; }
}
public class List
{
public int dt { get; set; }
public Main main { get; set; }
public List<Weather> weather { get; set; }
public Clouds clouds { get; set; }
public Wind wind { get; set; }
public Rain rain { get; set; }
public Sys sys { get; set; }
public string dt_txt { get; set; }
}
public class Coord
{
public double lat { get; set; }
public double lon { get; set; }
}
public class City
{
public int id { get; set; }
public string name { get; set; }
public Coord coord { get; set; }
public string country { get; set; }
}
public class RootObject
{
public string cod { get; set; }
public double message { get; set; }
public int cnt { get; set; }
public List<List> list { get; set; }
public City city { get; set; }
}
`
这是getweatherforcast按钮的点击事件(我添加了一个按钮来获取预测)
private async void ForecastButton_Click(object sender, RoutedEventArgs e)
{
var position1 = await LocationManager.GetPosition();
var latitude1 = position1.Coordinate.Latitude;
var longitude1 = position1.Coordinate.Longitude;
UWPWeatherforMobileForeCast.RootObject forecast = await WeatherForecast.GetWeatherForecast(latitude1, longitude1);
}
这是我要绑定数据的Gridview
<GridView x:Name="ForecastGridView" >
<GridView.ItemTemplate>
<DataTemplate >
<StackPanel>
<TextBlock Name="forecastdatetextblock"/>
<TextBlock Name="forecasttemptextblock" />
<TextBlock Name="forecastdescriptiontextblock"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
如何将 5 天的预测日期、温度和描述绑定到此网格视图。我应该使用什么样的 itemsource 和数据类型?
This is a screenshot of my app 这是一个非常简单的应用程序,用于我的教育目的。我想将日期、温度、描述绑定到我绘制的那个区域的网格视图上。
附言这是我的第一个stackoverflow问题,如果有任何错误请见谅
【问题讨论】:
-
您是开始使用 JSON 获取数据还是只是将 JSON 转换为 C#?如果是,请发布您获取 JSON 的方法,我们将为您提供帮助……如果不是,我们不会为您编写代码。下次请更明确:)
-
对不起,我的错。我添加了我写的代码。
-
我想这会对你有所帮助openweathermap.org/forecast5
-
你得到的只是一个数据模型。准备 ViewModel(例如 MyViewViewModel),然后在内部创建集合,例如IEnumerable
。这将为您提供灵活性,并且您将不依赖于您获得的数据。然后,您将能够使用例如填充集合。从您获取的模型开始 5 天。
标签: c# uwp openweathermap