using CentralControl.DBDAO; using CommonFunc; using CommonFunc.Tools; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; using System.Timers; 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.Shapes; using XGL.Views; namespace XGL.FormItem { /// /// Main.xaml 的交互逻辑 /// public partial class Main : Window { public static Main main; Frame frame1 = new Frame() { Content = new Views.LanJu_Index()}; Timer timer = new Timer(); System.Threading.Timer Time { get; set; } private string _PingSeconds; public string PingSeconds { get { return _PingSeconds; } set { OnPropertyChanged("PingSeconds"); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string info) { var handler = PropertyChanged; handler?.Invoke(this, new PropertyChangedEventArgs(info)); } public enum WindowID { frame1 } public Main() { InitializeComponent(); main = this; //WindowChange(WindowID.frame1); CommonFunc.Common.ReadConfig();//读取配置文件 timer.Interval = 1000; timer.Elapsed += Timer_Elapsed; timer.Start(); //实例化绑定对象 Binding textBinding = new Binding(); //设置要绑定源 textBinding.Source = this;//绑定MainWindow类 //设置要绑定属性 textBinding.Path = new PropertyPath("PingSeconds");//绑定MainWindow类下的TxtValue属性。 textBinding.Mode = BindingMode.TwoWay;//绑定模式双向绑定 //设置绑定到要绑定的控件 this.myTb.SetBinding(TextBlock.TextProperty, textBinding); InitPage(); GetServiceDelay(Utils.GetServerIP()); } private void InitPage() { if (Utils.GetAppSetting("ClientMode").Equals("0")) { LanJu_Prepare prepare = new LanJu_Prepare(); this.Index.Content = prepare; } else if (Utils.GetAppSetting("ClientMode").Equals("1")) { LanJu_Operator op = new LanJu_Operator(); this.Index.Content = op; } else { DryingRoomUC dryingRoomUC = new DryingRoomUC(); this.Index.Content = dryingRoomUC; } } private void Timer_Elapsed(object sender, ElapsedEventArgs e) { this.Dispatcher.Invoke( new Action(() => { LabDate.Content = DateTime.Now.ToString("yyyy-MM-dd"); LabTime.Content = DateTime.Now.ToString("HH:mm:ss"); } ), System.Windows.Threading.DispatcherPriority.Render) ; } public void WindowChange(WindowID windowID) { this.Index.Content = frame1; } private void Close(object sender, EventArgs e) { System.Environment.Exit(System.Environment.ExitCode); } private void btnExit_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show("是否确认关闭程序", "提示信息", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.No) { } else { System.Environment.Exit(System.Environment.ExitCode); } } private void btnZXH_Click(object sender, RoutedEventArgs e) { this.WindowState = WindowState.Minimized; } private void Window_Loaded(object sender, RoutedEventArgs e) { this.tbOperator.Text = LoginUser.UserName; this.tbDevice.Text = Utils.GetAppSetting("DeviceCode"); string sitecode = Utils.GetAppSetting("SiteCode"); DBService userDbWareHouse = new DBService(); DataTable dt = userDbWareHouse.GetFactoryInfo(sitecode); string name = dt == null?"": dt.Rows[0][0].ToString(); this.tbFactory.Text = name; } public void GetServiceDelay(string serviceIp) { Ping p1 = new Ping(); Time = new System.Threading.Timer((s) => { try { p1.SendAsyncCancel(); p1.SendPingAsync(serviceIp, 2000).ContinueWith(result => { try { string time = result?.Result?.RoundtripTime.ToString(); Application.Current.Dispatcher.Invoke(() => { if (!string.IsNullOrEmpty(time) && time != "0") { _PingSeconds = time; PingSeconds = time; this.btnText.Text = time; } else { _PingSeconds = time; PingSeconds = "--"; this.btnText.Text = "--"; } int second = 0; if (int.TryParse(time, out second)) { if (second > 0 && second <= 20) { this.btnText.Foreground = Brushes.Green; //System.Windows.Media.ColorConverter.ConvertFromString("#FF4FEE4F"); } else if (second > 20 && second <= 40) { this.btnText.Foreground = Brushes.Orange; } else { this.btnText.Foreground = Brushes.Red; } } else { this.btnText.Foreground = Brushes.Red; } }); } catch (Exception ex) { result?.Dispose(); } }); } catch (Exception) { p1.SendAsyncCancel(); } }, 1, 0, 2000); } } }