- using System;
- using System.ComponentModel;
- using System.Net.Http;
- using System.Threading.Tasks;
- using System.Windows;
- using Newtonsoft.Json;
- namespace WeatherApp
- {
- public partial class MainWindow : Window, INotifyPropertyChanged
- {
- private const string ApiKey = "3b5480b3ea2e84262e5a0b4117b93ccd";
- private const string BaseUrl = "https://api.openweathermap.org/data/2.5";
- private WeatherData _currentWeather;
- private string _currentDayOfWeek;
- public WeatherData CurrentWeather
- {
- get { return _currentWeather; }
- set
- {
- _currentWeather = value;
- OnPropertyChanged("CurrentWeather");
- if (_currentWeather != null && _currentWeather.Weather.Length > 0)
- {
- string weatherIconUrl = $"http://openweathermap.org/img/wn/{_currentWeather.Weather[0].Icon}.png";
- imgWeatherIcon.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(weatherIconUrl));
- imgWeatherIcon.Visibility = Visibility.Visible;
- }
- else
- {
- imgWeatherIcon.Visibility = Visibility.Collapsed;
- }
- }
- }
- public string CurrentDayOfWeek
- {
- get { return _currentDayOfWeek; }
- set
- {
- _currentDayOfWeek = value;
- OnPropertyChanged("CurrentDayOfWeek");
- }
- }
- public MainWindow()
- {
- InitializeComponent();
- DataContext = this;
- CurrentDayOfWeek = DateTime.Now.DayOfWeek.ToString();
- }
- private async void BtnSearch_Click(object sender, RoutedEventArgs e)
- {
- string cityName = txtSearch.Text.Trim();
- if (!string.IsNullOrEmpty(cityName))
- {
- await GetWeatherData(cityName);
- }
- }
- private async Task GetWeatherData(string cityName)
- {
- try
- {
- using (HttpClient client = new HttpClient())
- {
- string currentWeatherUrl = $"{BaseUrl}/weather?q={cityName}&appid={ApiKey}&units=metric";
- HttpResponseMessage currentWeatherResponse = await client.GetAsync(currentWeatherUrl);
- string currentWeatherJson = await currentWeatherResponse.Content.ReadAsStringAsync();
- CurrentWeather = JsonConvert.DeserializeObject<WeatherData>(currentWeatherJson);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"Error occurred: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public class WeatherData
- {
- [JsonProperty("name")]
- public string City { get; set; }
- [JsonProperty("main")]
- public MainInfo Main { get; set; }
- [JsonProperty("wind")]
- public WindInfo Wind { get; set; }
- public WeatherInfo[] Weather { get; set; }
- public string DayOfWeek { get; set; }
- }
- public class MainInfo
- {
- [JsonProperty("temp")]
- public double Temperature { get; set; }
- [JsonProperty("humidity")]
- public int Humidity { get; set; }
- [JsonProperty("pressure")]
- public int Pressure { get; set; }
- }
- public class WindInfo
- {
- [JsonProperty("speed")]
- public double Speed { get; set; }
- }
- public class WeatherInfo
- {
- [JsonProperty("icon")]
- public string Icon { get; set; }
- }
- }
[text] Дени
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- Дени
- Paste Code
- 09 Jun-2023
- 3.98 Kb
You can Share it: