如何从 openweathermap 获取 5 天天气预报到我的 uwp 应用程序

How to get 5day weather forecast from openweathermap to my uwp app

我不是经验丰富的开发人员。我只是编程的菜鸟。

我已经通过此 url http://samples.openweathermap.org/data/2.5/weather?lat=7.0744&lon=79.8919&appid=a7cae8ecfab2535dec05a83525f5ac7a

成功地将当前天气信息获取到我的应用程序

但我不知道如何从这个 openweathermap 中获取 5 天的天气预报数据 url http://samples.openweathermap.org/data/2.5/forecast?lat=7.0744&lon=79.8919&appid=a7cae8ecfab2535dec05a83525f5ac7a

这是我创建的天气预报class

 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 天预报日期、温度和描述绑定到此 gridview。我应该使用哪种项目源和数据类型?

This is a screenshot of my app 对于我的教育目的来说,这是一个非常简单的应用程序。我想将日期、温度、描述绑定到我绘制的那个区域中的 gridview。

p.s。这是我的第一个Whosebug问题所以如果有任何错误请原谅我

How to bind 5day forecast date,temp and description to this gridview. what kind of itemsource and data type should I use?

在按钮点击事件中获取到RootObject对象后,需要对数据进行操作,得到五天的天气,并显示在UI上。这是一个简单的示例代码,将5天的天气绑定到UI,您可以参考。 (点击按钮获取5天是否数据并显示)

这是xaml

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button Content="get whether" Click="Button_Click"/>
    <GridView ItemsSource="{Binding collection}" x:Name="ForecastGridView">
        <GridView.ItemTemplate>
            <DataTemplate >
                <StackPanel>
                    <TextBlock Name="forecastdatetextblock" Text="{Binding dt}"/>
                    <TextBlock Name="forecasttemptextblock" Text="{Binding main.temp}" />
                    <TextBlock Name="forecastdescriptiontextblock"  Text="{Binding weather[0].description}"/>
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
</StackPanel>

这是后面的代码

public MainPage()
{
    this.InitializeComponent();
    collection = new ObservableCollection<List>();
    this.DataContext = this;
}

public ObservableCollection<List> collection { get; set; }
private async void Button_Click(object sender, RoutedEventArgs e)
{
    var position1 = await LocationManager.GetPosition();
    var latitude1 = position1.Coordinate.Latitude;
    var longitude1 = position1.Coordinate.Longitude;
    RootObject forecast = await WeatherForecast.GetWeatherForecast(latitude1, longitude1);

    for (int i = 0; i < 5; i++)
    {

        collection.Add(forecast.list[i]);
    }

    ForecastGridView.ItemsSource = collection;

}