MainWindow.xaml.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using MV485.db;
  2. using MV485.Dlg;
  3. using MV485.helper;
  4. using MV485.model;
  5. using MV485.uc;
  6. using MV485.Upgrade;
  7. using MV485.util;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.IO.Ports;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. namespace MV485
  25. {
  26. /// <summary>
  27. /// MainWindow.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class MainWindow : Window
  30. {
  31. private string _titleInfo = "";
  32. private SolidColorBrush SelectedForeground;
  33. //字典用于缓存已加载的页面
  34. private Dictionary<string, object> pageCache = new Dictionary<string, object>();
  35. public MainWindow()
  36. {
  37. InitializeComponent();
  38. this.Title = $"{this.Title} V{UpgradeHelper.GetCurrentVersion()}";
  39. _titleInfo = this.Title;
  40. Loaded += MainWindow_Loaded;
  41. SelectedForeground = Brushes.DarkGreen;
  42. this.DataContext = this;
  43. }
  44. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  45. {
  46. //throw new NotImplementedException();
  47. this.Title = $"{this.Title} ({(LicenseMana.mIsLicensed ? "已注册" : "未注册")})";
  48. Dispatcher.BeginInvoke(new Action(() =>
  49. {
  50. DBSlave.FreeDatabase();
  51. }));
  52. }
  53. private void BtnDataMonitor_Click(object sender, RoutedEventArgs e)
  54. {
  55. ResetButtonForegrounds();
  56. txtDataMonitor.Foreground = SelectedForeground;
  57. string key = "data_monitor";
  58. if (pageCache.ContainsKey(key))
  59. {
  60. ContentArea.Content = pageCache[key];
  61. }
  62. else
  63. {
  64. var page = new UCMonitorData2();
  65. pageCache[key] = page; //缓存页面
  66. ContentArea.Content = page;
  67. }
  68. }
  69. private void BtnDeviceConfig_Click(object sender, RoutedEventArgs e)
  70. {
  71. ResetButtonForegrounds();
  72. txtDeviceConfig.Foreground = SelectedForeground;
  73. string key = "device_config";
  74. if (pageCache.ContainsKey(key))
  75. {
  76. ContentArea.Content = pageCache[key];
  77. }
  78. else
  79. {
  80. var page = new UCDeviceConfig();
  81. pageCache[key] = page; //缓存页面
  82. ContentArea.Content = page;
  83. }
  84. }
  85. private void BtnRunConfig_Click(object sender, RoutedEventArgs e)
  86. {
  87. ResetButtonForegrounds();
  88. txtRunConfig.Foreground = SelectedForeground;
  89. string key = "run_config";
  90. if (pageCache.ContainsKey(key))
  91. {
  92. ContentArea.Content = pageCache[key];
  93. }
  94. else
  95. {
  96. var page = new UCRunConfig();
  97. pageCache[key] = page; //缓存页面
  98. ContentArea.Content = page;
  99. }
  100. }
  101. private void BtnDeviceFind_Click(object sender, RoutedEventArgs e)
  102. {
  103. ResetButtonForegrounds();
  104. txtDeviceFind.Foreground = SelectedForeground;
  105. string key = "device_find";
  106. if (pageCache.ContainsKey(key))
  107. {
  108. ContentArea.Content = pageCache[key];
  109. }
  110. else
  111. {
  112. var page = new UCDeviceFinder();
  113. pageCache[key] = page; //缓存页面
  114. ContentArea.Content = page;
  115. }
  116. }
  117. private void BtnDeviceUpgrad_Click(object sender, RoutedEventArgs e)
  118. {
  119. ResetButtonForegrounds();
  120. txtDeviceUpgrad.Foreground = SelectedForeground;
  121. string key = "device_upgrad";
  122. if (pageCache.ContainsKey(key))
  123. {
  124. ContentArea.Content = pageCache[key];
  125. }
  126. else
  127. {
  128. var page = new UCDeviceUpgrade();
  129. pageCache[key] = page; //缓存页面
  130. ContentArea.Content = page;
  131. }
  132. }
  133. private void BtnReg_Click(object sender, RoutedEventArgs e)
  134. {
  135. RegisterDlg registerDlg = new RegisterDlg
  136. {
  137. Owner = this,
  138. WindowStartupLocation = WindowStartupLocation.CenterOwner
  139. };
  140. registerDlg.RegistSuccess += (blRegister) =>
  141. {
  142. this.Title = $"{_titleInfo} ({(LicenseMana.mIsLicensed ? "已注册" : "未注册")})";
  143. };
  144. if (registerDlg.ShowDialog() == true)
  145. {
  146. }
  147. }
  148. private void BtnAbout_Click(object sender, RoutedEventArgs e)
  149. {
  150. AboutWindow aboutWindow = new AboutWindow()
  151. {
  152. Owner = Application.Current.MainWindow
  153. };
  154. aboutWindow.ShowDialog(); // 显示为模态窗口
  155. }
  156. private void ResetButtonForegrounds()
  157. {
  158. txtDataMonitor.Foreground = Brushes.Black;
  159. txtDeviceConfig.Foreground = Brushes.Black;
  160. txtRunConfig.Foreground = Brushes.Black;
  161. txtDeviceFind.Foreground = Brushes.Black;
  162. txtDeviceUpgrad.Foreground = Brushes.Black;
  163. }
  164. private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  165. {
  166. // 查找所有打开的窗口
  167. foreach (Window child in Application.Current.Windows)
  168. {
  169. // 检查是否是你想要的窗口
  170. if (child is DlgImage || child is DlgExample)
  171. {
  172. child.Close();
  173. }
  174. }
  175. }
  176. private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
  177. {
  178. // 当按下 Esc 键时关闭对话框
  179. if (e.Key == Key.Escape)
  180. {
  181. foreach (Window child in Application.Current.Windows)
  182. {
  183. // 检查是否是你想要的窗口
  184. if (child is DlgImage || child is DlgExample)
  185. {
  186. child.Close();
  187. }
  188. }
  189. }
  190. }
  191. private void BtnCheckUpgrade_Click(object sender, RoutedEventArgs e)
  192. {
  193. WaitWindow waitWindow = null;
  194. try
  195. {
  196. string titleInfo = "正在检查升级信息,请稍后...";
  197. waitWindow = new WaitWindow(titleInfo)
  198. {
  199. Owner = Application.Current.MainWindow,
  200. WindowStartupLocation = WindowStartupLocation.CenterOwner
  201. };
  202. waitWindow.Show();
  203. UpgradeModel upgradeModel = UpgradeHelper.ReadUpdateTxt();
  204. //waitWindow.Close();
  205. //获取当前的版本与versionCode比较
  206. string currentVersion = UpgradeHelper.GetCurrentVersion();
  207. // 获取服务器返回的 versionCode(假设是字符串 "1.0.0.6")
  208. string serverVersionCode = upgradeModel.VersionCode;
  209. if (UpgradeHelper.IsNewVersionAvailable(currentVersion, serverVersionCode))
  210. {
  211. //MessageBox.Show($"发现新版本({serverVersionCode})。请升级应用。", "升级提示",
  212. // MessageBoxButton.OK, MessageBoxImage.Information);
  213. WndUpdateAsk updateAsk = new WndUpdateAsk(upgradeModel)
  214. {
  215. Owner = Application.Current.MainWindow,
  216. WindowStartupLocation = WindowStartupLocation.CenterOwner
  217. };
  218. updateAsk.ShowDialog();
  219. }
  220. else
  221. {
  222. MessageBox.Show("当前已经是最新版本。", "无升级", MessageBoxButton.OK, MessageBoxImage.Information);
  223. }
  224. }
  225. catch (Exception ex)
  226. {
  227. MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  228. }
  229. if (waitWindow != null)
  230. {
  231. waitWindow.Close();
  232. }
  233. }
  234. //------------------------------------------------
  235. }
  236. }