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

我已设法通过此URL http://samples.openweathermap.org/data/2.5/weather?lat=7.0744&lon=79.8919&appid=a7cae8ecfab2535dec05a83525f5ac7a将当前天气信息发送到我的应用程序

但我不知道如何从此openweathermap网址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按钮的click事件(我添加了一个按钮来进行预测)

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

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

ps。这是我的第一个stackoverflow问题,如果有任何错误,请原谅我

最佳答案

如何将5天预测日期,温度和描述绑定到此表格视图。我应该使用哪种项目源和数据类型?


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

}

关于c# - 如何从openweathermap获取5天天气预报到我的uwp应用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47589249/

10-12 15:26