123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using MV485.db;
- using MV485.Dlg;
- using MV485.helper;
- using MV485.model;
- using MV485.uc;
- using MV485.Upgrade;
- using MV485.util;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO.Ports;
- 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;
- namespace MV485
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- private string _titleInfo = "";
-
- private SolidColorBrush SelectedForeground;
- //字典用于缓存已加载的页面
- private Dictionary<string, object> pageCache = new Dictionary<string, object>();
- public MainWindow()
- {
- InitializeComponent();
- this.Title = $"{this.Title} V{UpgradeHelper.GetCurrentVersion()}";
- _titleInfo = this.Title;
- Loaded += MainWindow_Loaded;
-
- SelectedForeground = Brushes.DarkGreen;
- this.DataContext = this;
- }
- private void MainWindow_Loaded(object sender, RoutedEventArgs e)
- {
- //throw new NotImplementedException();
- this.Title = $"{this.Title} ({(LicenseMana.mIsLicensed ? "已注册" : "未注册")})";
- Dispatcher.BeginInvoke(new Action(() =>
- {
- DBSlave.FreeDatabase();
- }));
- }
- private void BtnDataMonitor_Click(object sender, RoutedEventArgs e)
- {
- ResetButtonForegrounds();
- txtDataMonitor.Foreground = SelectedForeground;
- string key = "data_monitor";
- if (pageCache.ContainsKey(key))
- {
- ContentArea.Content = pageCache[key];
- }
- else
- {
- var page = new UCMonitorData2();
- pageCache[key] = page; //缓存页面
- ContentArea.Content = page;
- }
- }
- private void BtnDeviceConfig_Click(object sender, RoutedEventArgs e)
- {
- ResetButtonForegrounds();
- txtDeviceConfig.Foreground = SelectedForeground;
- string key = "device_config";
- if (pageCache.ContainsKey(key))
- {
- ContentArea.Content = pageCache[key];
- }
- else
- {
- var page = new UCDeviceConfig();
- pageCache[key] = page; //缓存页面
- ContentArea.Content = page;
- }
- }
- private void BtnRunConfig_Click(object sender, RoutedEventArgs e)
- {
- ResetButtonForegrounds();
- txtRunConfig.Foreground = SelectedForeground;
- string key = "run_config";
- if (pageCache.ContainsKey(key))
- {
- ContentArea.Content = pageCache[key];
- }
- else
- {
- var page = new UCRunConfig();
- pageCache[key] = page; //缓存页面
- ContentArea.Content = page;
- }
- }
- private void BtnDeviceFind_Click(object sender, RoutedEventArgs e)
- {
- ResetButtonForegrounds();
- txtDeviceFind.Foreground = SelectedForeground;
- string key = "device_find";
- if (pageCache.ContainsKey(key))
- {
- ContentArea.Content = pageCache[key];
- }
- else
- {
- var page = new UCDeviceFinder();
- pageCache[key] = page; //缓存页面
- ContentArea.Content = page;
- }
- }
- private void BtnDeviceUpgrad_Click(object sender, RoutedEventArgs e)
- {
- ResetButtonForegrounds();
- txtDeviceUpgrad.Foreground = SelectedForeground;
- string key = "device_upgrad";
- if (pageCache.ContainsKey(key))
- {
- ContentArea.Content = pageCache[key];
- }
- else
- {
- var page = new UCDeviceUpgrade();
- pageCache[key] = page; //缓存页面
- ContentArea.Content = page;
- }
- }
- private void BtnReg_Click(object sender, RoutedEventArgs e)
- {
- RegisterDlg registerDlg = new RegisterDlg
- {
- Owner = this,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- registerDlg.RegistSuccess += (blRegister) =>
- {
- this.Title = $"{_titleInfo} ({(LicenseMana.mIsLicensed ? "已注册" : "未注册")})";
- };
- if (registerDlg.ShowDialog() == true)
- {
- }
- }
- private void BtnAbout_Click(object sender, RoutedEventArgs e)
- {
- AboutWindow aboutWindow = new AboutWindow()
- {
- Owner = Application.Current.MainWindow
- };
- aboutWindow.ShowDialog(); // 显示为模态窗口
- }
- private void ResetButtonForegrounds()
- {
- txtDataMonitor.Foreground = Brushes.Black;
- txtDeviceConfig.Foreground = Brushes.Black;
- txtRunConfig.Foreground = Brushes.Black;
- txtDeviceFind.Foreground = Brushes.Black;
- txtDeviceUpgrad.Foreground = Brushes.Black;
- }
- private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
- {
- // 查找所有打开的窗口
- foreach (Window child in Application.Current.Windows)
- {
- // 检查是否是你想要的窗口
- if (child is DlgImage || child is DlgExample)
- {
- child.Close();
- }
- }
- }
- private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- // 当按下 Esc 键时关闭对话框
- if (e.Key == Key.Escape)
- {
- foreach (Window child in Application.Current.Windows)
- {
- // 检查是否是你想要的窗口
- if (child is DlgImage || child is DlgExample)
- {
- child.Close();
- }
- }
- }
- }
- private void BtnCheckUpgrade_Click(object sender, RoutedEventArgs e)
- {
- WaitWindow waitWindow = null;
- try
- {
- string titleInfo = "正在检查升级信息,请稍后...";
- waitWindow = new WaitWindow(titleInfo)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- waitWindow.Show();
- UpgradeModel upgradeModel = UpgradeHelper.ReadUpdateTxt();
- //waitWindow.Close();
- //获取当前的版本与versionCode比较
- string currentVersion = UpgradeHelper.GetCurrentVersion();
- // 获取服务器返回的 versionCode(假设是字符串 "1.0.0.6")
- string serverVersionCode = upgradeModel.VersionCode;
- if (UpgradeHelper.IsNewVersionAvailable(currentVersion, serverVersionCode))
- {
- //MessageBox.Show($"发现新版本({serverVersionCode})。请升级应用。", "升级提示",
- // MessageBoxButton.OK, MessageBoxImage.Information);
- WndUpdateAsk updateAsk = new WndUpdateAsk(upgradeModel)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- updateAsk.ShowDialog();
- }
- else
- {
- MessageBox.Show("当前已经是最新版本。", "无升级", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- if (waitWindow != null)
- {
- waitWindow.Close();
- }
- }
- //------------------------------------------------
- }
- }
|