using Cowain.Bake.BLL; using Cowain.Bake.Common.Core; using Cowain.Bake.Common.Interface; using Cowain.Bake.Main.Station; using Prism.Ioc; using Prism.Modularity; using Prism.Regions; using Prism.Unity; using System.Threading.Tasks; using System; using System.Windows; using Unity; using Cowain.Bake.Common; using Cowain.Bake.Main.Views; using Cowain.Bake.Main.ViewModels; using Cowain.Bake.Main.Common; using Cowain.Bake.Communication.MOM; using Cowain.Bake.Main.Models; using System.Reflection; using AutoUpdaterDotNET; using System.Diagnostics; using Prism.Services.Dialogs; namespace Cowain.Bake.Main { /// /// App.xaml 的交互逻辑 /// public partial class App : PrismApplication { protected override Window CreateShell() //3 { //UI线程未捕获异常处理事件 this.DispatcherUnhandledException += OnDispatcherUnhandledException; //Task线程内未捕获异常处理事件 TaskScheduler.UnobservedTaskException += OnUnobservedTaskException; //多线程异常 AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; this.Exit += App_Exit; if (SettingProvider.Instance.AutoUpdate) { AutoUpdaterFun(); } CheckExistProcess(); //IsAskPath(); //正式运行,要放开这, return Container.Resolve(); } private void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { //通常全局异常捕捉的都是致命信息 LogHelper.Instance.GetCurrentClassFatal($"{e.Exception.StackTrace},{e.Exception.Message}"); } public static Process GetRunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) return process; } return null; } bool CheckExistProcess() { Process instance = GetRunningInstance(); if (instance != null)//没有已经开启的实例 { HandyControl.Controls.MessageBox.Warning("程序已在运行,若要重新运行程序请在后台关闭程序进程!", "警告"); Environment.Exit(0); return true; } return false; } //是否要求路径运行 bool IsAskPath() { string path = System.AppDomain.CurrentDomain.BaseDirectory; if (path.Contains(MyPath.MAIN_APP)) { return true; } HandyControl.Controls.MessageBox.Warning($"程序必须放在[{ MyPath.MAIN_APP}]路径下运行!", "警告"); Environment.Exit(0); return false; } private void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) { // 处理未观察到的异常 Console.WriteLine("Unobserved task exception occurred: " + e.Exception); LogHelper.Instance.GetCurrentClassFatal($"{e.Exception}"); // 获取异常的具体信息 if (e.Exception is AggregateException aggregateException) { foreach (var innerException in aggregateException.InnerExceptions) { LogHelper.Instance.GetCurrentClassFatal("Exception: " + innerException.Message); LogHelper.Instance.GetCurrentClassFatal("StackTrace: " + innerException.StackTrace); } } // 标记异常已被处理 e.SetObserved(); //LogHelper.Instance.GetCurrentClassFatal($"{e.Exception.StackTrace},{e.Exception.Message},{e.Exception.Source}"); } private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception ex = e.ExceptionObject as Exception; LogHelper.Instance.GetCurrentClassFatal($"{ex.StackTrace},{ex.Message}"); //记录dump文件 MiniDump.TryDump($"dumps\\CowainPreheat_{DateTime.Now.ToString("HH-mm-ss-ms")}.dmp"); } private void App_Exit(object sender, ExitEventArgs e) { //程序退出时需要处理的业务 } protected override void OnInitialized() //5 { Container.Resolve(); // 确保对话框服务已注册 20260121 var regionManager = Container.Resolve(); regionManager.RegisterViewWithRegion("MainHeaderRegion", typeof(Views.MainHeaderView)); regionManager.RegisterViewWithRegion("BasicInfoRegion", typeof(Views.BasicInfoView)); //regionManager.RegisterViewWithRegion("StationInfoRegion", typeof(EquipmentMonitorView)); Container.Resolve(); Container.Resolve(); Container.Resolve(); Container.Resolve(); //加了他会实例化二次 Container.Resolve(); Container.Resolve(); Container.Resolve(); Container.Resolve(); Container.Resolve(); Container.Resolve(); base.OnInitialized(); regionManager.RegisterViewWithRegion("StationInfoRegion", typeof(EquipmentMonitorView)); //Loaded,会触发一次 if (!HslCommunication.Authorization.SetAuthorizationCode("ac963114-3a46-4444-9a16-080a0ce99535")) { LogHelper.Instance.Fatal("PLC授权失败", true); } } protected override void InitializeShell(Window shell) { // 先打开登录窗口,如果登录成功,则打开主窗口 if (Container.Resolve().ShowDialog() == false) //4 { Environment.Exit(0); //Application.Current?.Shutdown(); } else { base.InitializeShell(shell); } } protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) //2 { // 可以改为自动扫描 moduleCatalog.AddModule(); moduleCatalog.AddModule(); moduleCatalog.AddModule(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) //1 { // 1. 把 Window 注册为“对话框外壳”——仅此一句 //containerRegistry.RegisterDialog(); //containerRegistry.RegisterDialog(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); //containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); } private void AutoUpdaterFun() { string updateXmlUrl = SettingProvider.Instance.AutoUpdateUrl; // 替换为你的实际 URL //AutoUpdater.AppTitle = "我的WPF应用程序"; AutoUpdater.Start(updateXmlUrl); //AutoUpdater.OpenDownloadPage = true; // 不打开浏览器 //AutoUpdater.UpdateMode = Mode.ForcedDownload; //常规模式,会显示标准更新对话框 //AutoUpdater.UpdateFormSize = new System.Drawing.Size(200, 100); AutoUpdater.Mandatory = true; // 强制更新 AutoUpdater.ShowSkipButton = false; // 隐藏跳过按钮 AutoUpdater.ShowRemindLaterButton = false; // 隐藏稍后提醒按钮 // 事件订阅 AutoUpdater.CheckForUpdateEvent += OnCheckForUpdate; AutoUpdater.ApplicationExitEvent += AutoUpdater_CheckForUpdateEvent; } private void AutoUpdater_CheckForUpdateEvent() { // 确保应用程序完全退出 System.Windows.Forms.Application.Exit(); } private void OnCheckForUpdate(UpdateInfoEventArgs args) { if (args.Error != null) { if (args.Error is System.Net.WebException) { // 网络错误,稍后重试 LogHelper.Instance.Fatal("无法连接更新服务器,请检查网络连接", true); } return; } if (!args.IsUpdateAvailable) { LogHelper.Instance.Fatal($"当前已是最新版本,服务器版本:{args.CurrentVersion},本程序版本:{args.InstalledVersion},本程序版本:" + Assembly.GetExecutingAssembly().GetName().Version); return; } MessageBox.Show($"发现新版本,服务器版本:{args.CurrentVersion},本程序版本:" + Assembly.GetExecutingAssembly().GetName().Version, "新版本", MessageBoxButton.OK, MessageBoxImage.Asterisk); // 可以在这里添加自定义逻辑,比如备份配置文件等 //BackupApplicationFiles(); // 自动开始下载更新 try { AutoUpdater.DownloadUpdate(args); Environment.Exit(0); //有用 } catch (Exception ex) { LogHelper.Instance.Fatal($"下载更新失败: {ex.Message}", true); } } } }