private void timer2_Tick(object sender, EventArgs e) { try { WebClient MyWebClient = new WebClient(); MyWebClient.Credentials = CredentialCache.DefaultCredentials; Byte[] pageData = MyWebClient.DownloadData("https://api.seniverse.com/v3/weather/now.json?key=SsqE832RAq3z3EfAE&location=lishui&language=zh-Hans"); weather = Encoding.UTF8.GetString(pageData); } catch { }; hweather.Root rt2 = JsonConvert.DeserializeObject <hweather.Root>(weather); string ab = " "; for (int i = 0; i < rt2.results.Count; i++) { if (i == 0) { ab = rt2.results[i].now.code; } } BitmapImage ImageSource2 = new BitmapImage(new Uri("/" + ab + "@2x.png", UriKind.Relative)); _1h.Source = ImageSource2; _1hc.Content = rt2.results[0].now.temperature + "℃"; }
private void Window_Loaded(object sender, RoutedEventArgs e) { string[] text = new string[4]; text[0] = "XBB2"; text[1] = "XINGXING"; text[2] = "LIUSHU"; text[3] = "Xcube"; Random ra = new Random(); TimeLable.Content = text[ra.Next(0, 3)]; string weather1h = " "; weathercardwindow.SetBinding(AcrylicPanel.TargetProperty, new Binding() { ElementName = "MainImage" }); if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "timeopen.txt")) { using (StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "timeopen.txt")) { string line; while ((line = sr.ReadLine()) != null) { a++; times[a] = line; } } } if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "timeclose.txt")) { using (StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "timeclose.txt")) { string line; while ((line = sr.ReadLine()) != null) { b++; timeclose[b] = line; } } } if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "timedoing.txt")) { using (StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "timedoing.txt")) { string line; while ((line = sr.ReadLine()) != null) { c++; timecmd[c] = line; line = sr.ReadLine(); cmd[c] = line; } } } var desktopWorkingArea = System.Windows.SystemParameters.WorkArea; this.Left = desktopWorkingArea.Right - Width; this.Top = 0; DayLable.Content = DateTime.Now.ToString("yyyy年MM月dd日"); try { WebClient MyWebClient = new WebClient(); MyWebClient.Credentials = CredentialCache.DefaultCredentials; Byte[] pageData = MyWebClient.DownloadData("https://api.seniverse.com/v3/weather/daily.json?key=SsqE832RAq3z3EfAE&location=lishui&language=zh-Hans&unit=c&start=0&days=2"); weather = Encoding.UTF8.GetString(pageData); } catch { }; Root rt = JsonConvert.DeserializeObject <Root>(weather); for (int i = 0; i < rt.results.Count; i++) { for (int j = 0; j < rt.results[i].daily.Count; j++) { if (j == 0) { weather_day = rt.results[i].daily[j].code_day; weather_night = rt.results[i].daily[j].code_night; weather_low = rt.results[i].daily[j].low; weather_high = rt.results[i].daily[j].high; } } } try { WebClient MyWebClient = new WebClient(); MyWebClient.Credentials = CredentialCache.DefaultCredentials; Byte[] pageData = MyWebClient.DownloadData("https://api.seniverse.com/v3/weather/now.json?key=SsqE832RAq3z3EfAE&location=lishui&language=zh-Hans"); weather1h = Encoding.UTF8.GetString(pageData); } catch { }; hweather.Root rt2 = JsonConvert.DeserializeObject <hweather.Root>(weather1h); BitmapImage ImageSource = new BitmapImage(new Uri("/" + weather_day + "@2x.png", UriKind.Relative)); day.Source = ImageSource; BitmapImage ImageSource1 = new BitmapImage(new Uri("/" + weather_night + "@2x.png", UriKind.Relative)); night.Source = ImageSource1; high.Content = weather_high + "℃"; low.Content = weather_low + "℃"; string ab = " "; for (int i = 0; i < rt2.results.Count; i++) { if (i == 0) { ab = rt2.results[i].now.code; } } BitmapImage ImageSource2 = new BitmapImage(new Uri("/" + ab + "@2x.png", UriKind.Relative)); _1h.Source = ImageSource2; _1hc.Content = rt2.results[0].now.temperature + "℃"; DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(1); timer.Tick += timer1_Tick; timer.Start(); IntPtr handle = new WindowInteropHelper(this).Handle; SetWindowPos(handle, -1, 0, 0, 0, 0, 1 | 2); DispatcherTimer timer2 = new DispatcherTimer(); timer2.Interval = TimeSpan.FromHours(1); timer2.Tick += timer2_Tick; timer2.Start(); DispatcherTimer timer3 = new DispatcherTimer(); timer3.Interval = TimeSpan.FromMilliseconds(17); timer3.Tick += timer3_Tick; timer3.Start(); }