I'm not an experienced developer. I'm just a noob to programming.
I've managed to get current weather info to my app via this url
but I don't know how to get 5day weather forecast data from this openweathermap url
this is the Weatherforecast class i created
class WeatherForecast { public async static Task<RootObject> GetWeatherForecast(double lat,double lon) { var httpn = new HttpClient(); var uri = String.Format("", 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; } } `
This is the the click event of getweatherforcast button (I added a button to get forcast)
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); } This is the Gridview I want to bind data
<GridView x:Name="ForecastGridView" > <GridView.ItemTemplate> <DataTemplate > <StackPanel> <TextBlock Name="forecastdatetextblock"/> <TextBlock Name="forecasttemptextblock" /> <TextBlock Name="forecastdescriptiontextblock"/> </StackPanel> </DataTemplate> </GridView.ItemTemplate> </GridView> How to bind 5day forecast date,temp and description to this gridview. what kind of itemsource and data type should I use?
This is a screenshot of my app it's a very simple app for my education purposes. I want to bind date,temp,description on to a gridview in that area I drawn.
p.s. this is my first stackoverflow question so forgive me if there any mistakes
41 Answer
How to bind 5day forecast date,temp and description to this gridview. what kind of itemsource and data type should I use?
After you get the RootObject object in the button click event, you should operate the data to get five days weather and make them display on the UI. Here is a simple example code to bind 5 days weather to the UI, you can have a reference. (Click the button to get the 5 day whether data and display them)
This is the 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> This is the code behind
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; }