Acceptance Criteria
Continue your completed 0.4 First Day application. Keep the shopping-list save and load features, extend the window layout, and add a button that retrieves weather using longitude and latitude.
Goal: Request information from an online service and display the result in your application.
Name the backup First Day 0.4 Backup LastName.
Keep the working folder and solution names unchanged.
Include Save Shopping List and Load Shopping List. Fix any existing errors before continuing.
Open-Meteo provides weather data through an API: an interface that lets an application request information from an online service. Its documentation lets you explore locations, weather variables, and the URL used to request data.
Latitude describes a location north or south of the equator. Longitude describes a location east or west of the prime meridian. Negative values represent south or west. Our window places longitude first, so read each label carefully.
Explore the daily precipitation variables. This assignment requests one day of precipitation hours, maximum precipitation probability, and totals for precipitation, showers, rain, and snowfall. The URL also specifies unit preferences. Read the returned daily_units values to identify the units for each result. The service returns JSON: text containing named values and lists. Your application will display that response directly.
FirstDayScenario/MainWindow.xamlSwitch to the XAML source view.
In MainWindow.xaml, locate the outer Weather StackPanel whose opening tag contains Grid.Row="1". Find its matching closing tag, after the rain checkbox and clothing button. The shopping-list StackPanel with Grid.Row="3" follows it.
Insert the following entire block immediately after the outer Weather panel's closing tag and before the shopping-list panel's opening tag. The new StackPanel must be a direct child of the existing Grid, alongside those two panels. The Grid already has a row definition for row 2, so you do not need to add or change any row definitions.
<StackPanel Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Top"> <StackPanel Orientation="Horizontal"> <TextBlock Text="Longitude:" Width="125" Margin="5,5,5,0"/> <TextBlock Text="Latitude:" Width="125" Margin="5,5,5,0"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="longitudeTextBox" Width="125" Margin="5"/> <TextBox x:Name="latitudeTextBox" Width="125" Margin="5"/> <Button x:Name="loadWeatherButton" HorizontalAlignment="Left" Content="Load weather" Width="125" Margin="5" Click="loadWeatherButton_Click" /> </StackPanel> <Border Margin="5" Height="125" BorderThickness="1" BorderBrush="Black"> <WebBrowser x:Name="BrowserControl"/> </Border> </StackPanel>

The new panel occupies Grid row 2. Its first horizontal StackPanel holds the coordinate labels; its second holds the matching text boxes and Load weather button. The Border contains the browser that will display the response.
MainWindow.xamlThe new loadWeatherButton_Click method has not been created yet. Complete the next task before building or running the application.
Keep the existing newStudentButton_Click, haveBreakfastButton_Click, fillBackpackButton_Click, chooseClothingButton_Click, saveShoppingListButton_Click, and loadShoppingListButton_Click methods and their implementations. Each XAML Click value must match its C# method name exactly, including capitalization.
Load weather button in the XAML designerOpen the Design view of MainWindow.xaml and double-click Load weather. Visual Studio creates the Click event handler and opens MainWindow.xaml.cs. The method must be named loadWeatherButton_Click, matching the supplied XAML exactly. If the method already exists, double-clicking opens it; do not add a duplicate.
The supplied code uses fully qualified type names. Keep your existing using statements, including System.IO from 0.4. No additional using statement or JSON parsing package is needed.
MainWindow classPlace this field immediately after the existing Student field and before the constructor. It belongs inside the MainWindow class, outside all methods. Keep the existing Breakfast and Student fields and Feedback attributes.
private static readonly System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient();This client sends requests to the weather service and is reused each time you click Load weather.
GetForecastAsync after the weather handlerPaste this complete method after the weather event handler and before the closing brace of the MainWindow class. Do not place it inside the constructor or another method.
private async System.Threading.Tasks.Task<string> GetForecastAsync()
{
string url = $"https://api.open-meteo.com/v1/forecast?latitude={this.latitudeTextBox.Text}&longitude={this.longitudeTextBox.Text}&daily=precipitation_hours,precipitation_probability_max,precipitation_sum,showers_sum,rain_sum,snowfall_sum&forecast_days=1&wind_speed_unit=mph&temperature_unit=fahrenheit&precipitation_unit=inch";
return await Client.GetStringAsync(url);
}The URL uses the text from the two coordinate TextBoxes. The latitude parameter must read latitudeTextBox.Text, and the longitude parameter must read longitudeTextBox.Text. Keep the URL on one line. GetStringAsync retrieves the response as a string. The async and await keywords allow the application to wait for the response without blocking the window.
loadWeatherButton_Click method with this methodInclude the method declaration and braces below. Notice the async keyword in the declaration. Do not paste a method inside another method, and do not replace the entire MainWindow class. Keep the constructor and every event handler from 0.4.
private async void loadWeatherButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
try
{
string json = await this.GetForecastAsync();
string html = "<html><head>"
+ "<meta charset=\"utf-8\">"
+ "</head><body>"
+ "<pre style=\"white-space:pre-wrap;word-wrap:break-word;\">"
+ System.Net.WebUtility.HtmlEncode(json)
+ "</pre></body></html>";
this.BrowserControl.NavigateToString(html);
}
catch (System.Exception ex)
{
this.BrowserControl.NavigateToString(
"<html><body><h1>Unable to retrieve the forecast</h1><p>"
+ System.Net.WebUtility.HtmlEncode(ex.Message)
+ "</p></body></html>");
}
}The event handler calls GetForecastAsync and stores the response in json. HtmlEncode makes the response safe to display as text inside an HTML page. NavigateToString displays that page in BrowserControl. If the request fails, the catch block displays an error in the same browser area. Weather loading does not change textOutput or the rain checkbox.
Correct errors before continuing. If a Click handler is missing, double-click its button in the designer to create or open the handler. Restore its implementation from your 0.4 backup when needed; an empty handler will not perform the previous behavior.
New studentVerify that Nou Xiong, the student ID, energy level, and hungry status appear. Click New student before Have breakfast or Fill backpack so that a Student object exists.
Have breakfastVerify that the output describes the breakfast and updated energy level and hungry status.
Fill backpackVerify that its previous behavior still works.
Verify both clothing outcomes from the previous assignment. Loading weather does not automatically change this checkbox.
Save Shopping List button from the previous assignmentType Notebook and Pens, one per line, into the shopping-list TextBox. Click Save shopping list. Verify that the output says Shopping list saved..
Load Shopping List button from the previous assignmentClose and restart the application. Verify that the shopping-list TextBox starts empty. Click Load shopping list. Notebook and Pens should reappear, and the output should say Shopping list loaded.. Clear the TextBox and click Load shopping list again without saving; the same items should return.
Type -87.6298 in Longitude and 41.8781 in Latitude. Use a period for the decimal separator. These coordinates identify a location in Chicago.
Load weather and inspect the resultWith an internet connection, the browser area should display the JSON response. Look for daily_units, daily, and the requested precipitation fields. Scroll within the browser area if needed. The values change over time; no particular forecast value is required. The textOutput area keeps its previous message.
Load weatherKeep the example longitude and change Latitude to 100. Click Load weather. The browser area should display Unable to retrieve the forecast with an error message because the request contains an invalid latitude. Restore Latitude to 41.8781 and verify that the JSON response loads again.
Include both Save Shopping List and Load Shopping List, as well as Load weather.
Verify that LastName.sln and the FirstDayScenario project folder are included.
Resubmit an updated ZIP when needed.