【问题标题】:How to get 5day weather forecast from openweathermap to my uwp app如何从 openweathermap 获取 5 天天气预报到我的 uwp 应用程序
【发布时间】: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


【解决方案1】:

如何将 5 天的预测日期、温度和描述绑定到此网格视图。我应该使用什么样的 itemsource 和数据类型?

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

}

【讨论】:

    猜你喜欢
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多