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
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
private string _titleInfo = "";
private SolidColorBrush SelectedForeground;
//字典用于缓存已加载的页面
private Dictionary pageCache = new Dictionary();
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();
}
}
//------------------------------------------------
}
}