[text] Дени

Viewer

copydownloadembedprintName: Дени
  1. using System;
  2. using System.ComponentModel;
  3. using System.Net.Http;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using Newtonsoft.Json;
  7.  
  8. namespace WeatherApp
  9. {
  10.     public partial class MainWindow : Window, INotifyPropertyChanged
  11.     {
  12.         private const string ApiKey = "3b5480b3ea2e84262e5a0b4117b93ccd";
  13.         private const string BaseUrl = "https://api.openweathermap.org/data/2.5";
  14.  
  15.         private WeatherData _currentWeather;
  16.         private string _currentDayOfWeek;
  17.  
  18.         public WeatherData CurrentWeather
  19.         {
  20.             get { return _currentWeather; }
  21.             set
  22.             {
  23.                 _currentWeather = value;
  24.                 OnPropertyChanged("CurrentWeather");
  25.                 
  26.  
  27.                 if (_currentWeather != null && _currentWeather.Weather.Length > 0)
  28.                 {
  29.                     string weatherIconUrl = $"http://openweathermap.org/img/wn/{_currentWeather.Weather[0].Icon}.png";
  30.                     imgWeatherIcon.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(weatherIconUrl));
  31.                     imgWeatherIcon.Visibility = Visibility.Visible;
  32.                 }
  33.                 else
  34.                 {
  35.                     imgWeatherIcon.Visibility = Visibility.Collapsed;
  36.                 }
  37.             }
  38.         }
  39.  
  40.         public string CurrentDayOfWeek
  41.         {
  42.             get { return _currentDayOfWeek; }
  43.             set
  44.             {
  45.                 _currentDayOfWeek = value;
  46.                 OnPropertyChanged("CurrentDayOfWeek");
  47.             }
  48.         }
  49.  
  50.         public MainWindow()
  51.         {
  52.             InitializeComponent();
  53.             DataContext = this;
  54.  
  55.             CurrentDayOfWeek = DateTime.Now.DayOfWeek.ToString();
  56.         }
  57.  
  58.         private async void BtnSearch_Click(object sender, RoutedEventArgs e)
  59.         {
  60.             string cityName = txtSearch.Text.Trim();
  61.             if (!string.IsNullOrEmpty(cityName))
  62.             {
  63.                 await GetWeatherData(cityName);
  64.             }
  65.         }
  66.  
  67.         private async Task GetWeatherData(string cityName)
  68.         {
  69.             try
  70.             {
  71.                 using (HttpClient client = new HttpClient())
  72.                 {
  73.                     string currentWeatherUrl = $"{BaseUrl}/weather?q={cityName}&appid={ApiKey}&units=metric";
  74.  
  75.                     HttpResponseMessage currentWeatherResponse = await client.GetAsync(currentWeatherUrl);
  76.                     string currentWeatherJson = await currentWeatherResponse.Content.ReadAsStringAsync();
  77.                     CurrentWeather = JsonConvert.DeserializeObject<WeatherData>(currentWeatherJson);
  78.                 }
  79.             }
  80.             catch (Exception ex)
  81.             {
  82.                 MessageBox.Show($"Error occurred: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  83.             }
  84.         }
  85.  
  86.         public event PropertyChangedEventHandler PropertyChanged;
  87.  
  88.         protected virtual void OnPropertyChanged(string propertyName)
  89.         {
  90.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  91.         }
  92.     }
  93.  
  94.     public class WeatherData
  95.     {
  96.         [JsonProperty("name")]
  97.         public string City { get; set; }
  98.  
  99.         [JsonProperty("main")]
  100.         public MainInfo Main { get; set; }
  101.  
  102.         [JsonProperty("wind")]
  103.         public WindInfo Wind { get; set; }
  104.  
  105.         public WeatherInfo[] Weather { get; set; }
  106.  
  107.         public string DayOfWeek { get; set; }
  108.     }
  109.  
  110.     public class MainInfo
  111.     {
  112.         [JsonProperty("temp")]
  113.         public double Temperature { get; set; }
  114.  
  115.         [JsonProperty("humidity")]
  116.         public int Humidity { get; set; }
  117.  
  118.         [JsonProperty("pressure")]
  119.         public int Pressure { get; set; }
  120.     }
  121.  
  122.     public class WindInfo
  123.     {
  124.         [JsonProperty("speed")]
  125.         public double Speed { get; set; }
  126.     }
  127.  
  128.     public class WeatherInfo
  129.     {
  130.         [JsonProperty("icon")]
  131.         public string Icon { get; set; }
  132.     }
  133. }
  134.  

Editor

You can edit this paste and save as new:


File Description
  • Дени
  • Paste Code
  • 09 Jun-2023
  • 3.98 Kb
You can Share it: