using Container.Business; using Container.Common; using Container.Viewmodel; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace Container.Win { /// /// WeatherControl.xaml 的交互逻辑 /// public partial class WeatherControl : UserControl { DispatcherTimer WeatherTimer = new DispatcherTimer();//停止播放视频的定时器 JService jService = new JService(); Class_Log log = new Class_Log(); string city = string.Empty; public WeatherControl() { InitializeComponent(); WeatherTimer.Interval = TimeSpan.FromMinutes(60);//每秒刷新一次 WeatherTimer.Tick += new EventHandler(WeatherTimer_Tick); } public void LoadWeather(string city,int type,double width,double height, string backgroundColor, string fontColor) { this.Width = width; this.Height = height; this.city = city; BrushConverter converter = new BrushConverter(); if (!string.IsNullOrEmpty(backgroundColor)) { Brush newFill = (Brush)converter.ConvertFromString(backgroundColor); gridMain.Background = newFill; } if (!string.IsNullOrEmpty(fontColor)) { Brush newFill = (Brush)converter.ConvertFromString(fontColor); txtFL1.Foreground = newFill; txtAQI1.Foreground = newFill; txtAQI2.Foreground = newFill; txtFL2.Foreground = newFill; txtPM1.Foreground = newFill; txtPM2.Foreground = newFill; txtsd1.Foreground = newFill; txtsd2.Foreground = newFill; txtTemp1.Foreground = newFill; txtTemp2.Foreground = newFill; txtTempZ1.Foreground = newFill; txtsdp1.Foreground = newFill; txtAQIA1.Foreground = newFill; txtPMpm1.Foreground = newFill; txtPMV1.Foreground = newFill; txtTempZ2.Foreground = newFill; txtsdp2.Foreground = newFill; txtAQIA2.Foreground = newFill; txtPMpm2.Foreground = newFill; txtPMV2.Foreground = newFill; } gridMain.Width = width; gridMain.Height = height; style1.Width = width; style1.Height = height; style2.Width = width; style2.Height = height; BindWeather(); if (type == 0) { style1.Visibility = Visibility.Visible; style2.Visibility = Visibility.Hidden; } else { style2.Visibility = Visibility.Visible; style1.Visibility = Visibility.Hidden; } WeatherTimer.Start(); } private void WeatherTimer_Tick(object sender, EventArgs e) { try { BindWeather(); } catch (Exception ex) { log.WriteLogFile(ex.Message, "error"); } } private void BindWeather() { try { string imgUrl = ""; WeatherViewModel weather = jService.GetEnvironmental(city); if (weather == null || string.IsNullOrEmpty(weather.status)) { this.Width = 0; this.Height = 0; return; } if (weather.status.Contains("晴")) { imgUrl = "weather_sum.png"; } else if (weather.status.Contains("云")) { imgUrl = "weather_cloud.png"; } else if (weather.status.Contains("雨")) { imgUrl = "weather_rian.png"; } else if (weather.status.Contains("雪")) { imgUrl = "weather_snow.png"; } else if (weather.status.Contains("阴") || weather.status.Contains("雾")) { imgUrl = "weather_cloud.png"; } else { imgUrl = "weather_sum.png"; } if (!string.IsNullOrEmpty(weather.temperature)) { txtTemp1.Text = Math.Floor(Convert.ToDouble(weather.temperature)).ToString(); txtTemp2.Text = Math.Floor(Convert.ToDouble(weather.temperature)).ToString(); } if (!string.IsNullOrEmpty(weather.pm25)) { txtPM1.Text = Math.Floor(Convert.ToDouble(weather.pm25)).ToString(); txtPM2.Text = Math.Floor(Convert.ToDouble(weather.pm25)).ToString(); } if (!string.IsNullOrEmpty(weather.humidity)) { txtsd1.Text = weather.humidity.Replace("%",""); txtsd2.Text = weather.humidity.Replace("%", ""); } if (!string.IsNullOrEmpty(weather.windPower)) { txtFL1.Text = weather.windPower.Substring(0,weather.windPower.IndexOf("级")); txtFL2.Text = weather.windPower.Substring(0,weather.windPower.IndexOf("级")); } if (!string.IsNullOrEmpty(weather.aqi)) { txtAQI1.Text = weather.aqi; txtAQI2.Text = weather.aqi; } Uri uri = new Uri("pack://application:,,,/Container;component/image/weather/" + imgUrl); ImageBrush ib = new ImageBrush(); ib.ImageSource = new BitmapImage(uri); this.imgWeather1.Source = ib.ImageSource; this.imgWeather2.Source = ib.ImageSource; } catch (Exception e) { this.Width = 0; this.Height = 0; } } public void Dispose() { if (WeatherTimer != null) WeatherTimer.Stop(); } } }