123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- using SCADA.CommonCtrl.WpfControl;
- using SCADA.CommonLib;
- using SCADA.CommonLib.Service;
- using SCADA_DAQ.Plugin.Core.AutoUpdater;
- using SCADA_DAQ.Plugin.CoreUI;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using System.Xml.Linq;
- namespace SCADA_DAQ
- {
- /// <summary>
- /// App.xaml 的交互逻辑
- /// </summary>
- public partial class App : Application, IApp
- {
- private static SystemControl systemControl;
- private string _sessionid;
- /// <summary>
- /// 会话ID
- /// </summary>
- public string SessionId => _sessionid ?? (_sessionid = Guid.NewGuid().ToString());
- internal Task updateCheckTask;
- /// <summary>
- /// 产品ID
- /// </summary>
- public string ProductId { get; } = System.Windows.Forms.Application.ProductName;
- /// <summary>
- /// 版本ID
- /// </summary>
- public string UidVersion { get; set; }
- /// <summary>
- /// 启动时间
- /// </summary>
- public DateTime StartTime { get; }
- /// <summary>
- ///
- /// </summary>
- public IServiceHost ServiceHost { get; set; }
- private App()
- {
- StartTime = DateTime.Now;
- Console.WriteLine($"App Start {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
- updateCheckTask = Task.Factory.StartNew(() =>
- {
- try
- {
- var _checkUpdate = true;
- if (File.Exists("UserConfig.xml"))
- {
- var xmlFile = XDocument.Load("UserConfig.xml");
- var node = xmlFile.Descendants("App.AutoUpdate").FirstOrDefault();
- var val = node?.Value;
- bool.TryParse(val, out _checkUpdate);
- }
- if (_checkUpdate)
- {
- var task = UpdateHelper.CheckUpdate();
- task.Wait();
- }
- }
- catch (Exception)
- {
- }
- });
- try
- {
- var appServer = Env.WebServerAddress.Value;
- Trace.WriteLine($"Server:{appServer}");
- var task = Task.Factory.StartNew(Env.ParameterInit);
- var win = new MainWindow();
- MainWindow = win;
- win.FrmClosingWait += Win_FrmClosingWait;
- systemControl = SystemControl.Create(this);
- var iconPath = "Content/Img/favicon.ico";
- if (File.Exists(iconPath))
- {
- MainWindow.Icon = new BitmapImage(new Uri(iconPath, UriKind.RelativeOrAbsolute));
- }
- MainWindow.Show();
- task.Wait();
- systemControl.SystemInit();
- }
- catch (Exception ex)
- {
- updateCheckTask?.Wait(10 * 1000);
- Trace.TraceError(ex.ToString());
- }
- }
- private void Win_FrmClosingWait(object sender, EventArgs e)
- {
- SystemControl.GetInstance().ApplicationDispose(0);
- }
- private void Application_Startup(object sender, StartupEventArgs e)
- {
- }
- private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
- {
- systemControl.ApplicationSessionEnding(this, e);
- }
- private void Application_Exit(object sender, ExitEventArgs e)
- {
- systemControl.ApplicationExit(e.ApplicationExitCode);
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public bool RestartHost()
- {
- return systemControl.RestartHost();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="runAs"></param>
- public void Restart(bool runAs = false)
- {
- MainWindow.Close();
- Exit += (s, e) =>
- {
- new Thread(() =>
- {
- Thread.Sleep(1000);
- string strAppFileName = Process.GetCurrentProcess().MainModule.FileName;
- Process myNewProcess = new Process();
- myNewProcess.StartInfo.FileName = strAppFileName;
- myNewProcess.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
- if (runAs == true)
- {
- myNewProcess.StartInfo.Verb = "runas";
- }
- myNewProcess.Start();
- }).Start();
- };
- }
- /// <summary>
- ///
- /// </summary>
- public void Restart()
- {
- Dispatcher.Invoke(new Action(() =>
- {
- Restart(false);
- }));
- }
- /// <summary>
- ///
- /// </summary>
- public void ReLoadLicense()
- {
- systemControl.LoadLicense();
- }
- /// <summary>
- /// 显示toast
- /// </summary>
- /// <param name="message">toast内容</param>
- /// <param name="toastType"></param>
- /// <param name="showTime">toast 时间 短:2500 长:3500</param>
- public void ShowToast(object message, ControlStyle toastType = ControlStyle.Default, int showTime = 2000)
- {
- Dispatcher.BeginInvoke(new Action(() =>
- {
- var toastItem = new ToastItem() { Message = message, ControlStyle = toastType };
- var showPannel = SCADA.CommonCtrl.WpfHelper.VisualHelper.GetChildObject<Panel>(MainWindow, "PART_ToastLayer");
- toastItem.Show(showPannel, showTime);
- }));
- }
- /// <summary>
- /// 显示短toast
- /// </summary>
- /// <param name="message">toast内容</param>
- /// <param name="toastType"></param>
- public void ShowShortToast(object message, ControlStyle toastType)
- {
- ShowToast(message, toastType, 2000);
- }
- /// <summary>
- /// 显示长toast
- /// </summary>
- /// <param name="message">toast内容</param>
- /// <param name="toastType"></param>
- public void ShowLongToast(object message, ControlStyle toastType)
- {
- ShowToast(message, toastType, 3500);
- }
- }
- }
|