如何从openweathermap获取5天天气预报到我的uwp app

时间:2017-12-01 08:22:02

标签: c# uwp openweathermap

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

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

获取当前天气信息给我的应用

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

这是我创建的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>

如何将5day预测日期,温度和描述绑定到此gridview。我应该使用什么样的项目源和数据类型?

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

P.S。这是我的第一个stackoverflow问题,请原谅我是否有任何错误

1 个答案:

答案 0 :(得分:0)

  

如何将5day预测日期,温度和描述绑定到此gridview。我应该使用什么样的项目源和数据类型?

在按钮点击事件中获得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;

}