Files
ACP/ACP/App.xaml.cs
T
2026-08-30 15:57:28 +08:00

193 lines
8.1 KiB
C#

using Common;
using Command;
using ACP.ViewModels;
using ACP.ViewModels.Dialogs;
using ACP.Views;
using ACP.Views;
using ACP.Views.Dialogs;
using Logger;
using Notifications.Wpf.Core;
using ORM;
using Service.Implement;
using Service.Interface;
using System.Configuration;
using System.Data;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using UIShare.PubEvent;
using static System.Runtime.InteropServices.JavaScript.JSType;
using UIShare.GlobalVariable;
using UIShare;
using System;
using DeviceCommand.Devices;
using DeviceCommand.Base;
using AutoMapper;
using Microsoft.Extensions.Logging.Abstractions;
using ACP.Profiles;
using Prism.Dialogs;
using UIShare.Helpers;
namespace ACP
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
//UI线程未捕获异常处理事件
this.DispatcherUnhandledException += OnDispatcherUnhandledException;
//Task线程内未捕获异常处理事件
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
////多线程异常
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
return Container.Resolve<ShellView>();
}
private void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
LoggerHelper.Error(e.Exception.Message, e.Exception.StackTrace);
}
private void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
LoggerHelper.Error(e.Exception.Message, e.Exception.StackTrace);
}
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//记录dump文件
Exception ex = e.ExceptionObject as Exception;
MiniDump.TryDump($"dumps\\Error_{DateTime.Now:yyyy-MM-dd HH-mm-ss-ms}.dmp", MiniDump.Option.WithFullMemory, ex);
}
protected override void OnInitialized()
{
// 向命令库 CommandDialog 注入弹窗能力(命令库是纯 .NET 类库,不引用 WPF,这里通过委托实现依赖倒置)
var dialogService = Container.Resolve<IDialogService>();
CommandDialog.弹窗处理器 = (弹窗类型, 弹窗详细, 是否阻塞, 自动关闭秒数, ct) =>
{
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
CancellationTokenRegistration registration = default;
if (是否阻塞)
{
registration = ct.Register(() => tcs.TrySetCanceled(ct));
}
// 命令可能在后台线程执行,统一切回 UI 线程弹窗
Application.Current.Dispatcher.Invoke(() =>
{
var param = new DialogParameters
{
{ "Title", 弹窗类型 switch
{
CommandDialog.DialogType.Warning => "警告",
CommandDialog.DialogType.Error => "错误",
_ => "信息提示"
} },
{ "Message", 弹窗详细 ?? string.Empty },
{ "Icon", 弹窗类型 switch
{
CommandDialog.DialogType.Warning => "warn",
CommandDialog.DialogType.Error => "error",
_ => "info"
} },
{ "ShowOk", true },
{ "AutoCloseSeconds", (double)自动关闭秒数 }
};
if (是否阻塞)
{
// 阻塞:用户手动关闭或自动关闭后才继续执行步骤
dialogService.ShowDialog("MessageBox", param, _ =>
{
registration.Dispose();
tcs.TrySetResult(true);
});
}
else
{
// 非阻塞:弹出后步骤立即继续(不等关闭回调)
dialogService.Show("MessageBox", param, _ => { });
tcs.TrySetResult(true);
}
});
return tcs.Task;
};
// 配置全局日志分发器:按 CurrentScope 路由到对应 LogArea
var globalInfo = Container.Resolve<GlobalInfo>();
LoggerHelper.Progress = new ScopeLogDispatcher(globalInfo);
//初始化数据库
DatabaseConfig.SetTenant(10001);
DatabaseConfig.InitSqlite();
DatabaseConfig.CreateDatabaseAndCheckConnection(createDatabase: true, checkConnection: true);
SqlSugarContext.InitDatabase();
//显示登录窗口
var login = Container.Resolve<LoginModuleView>();
var re = Container.Resolve<IRegionManager>();
RegionManager.SetRegionManager(login, re);
RegionManager.SetRegionManager(Application.Current.MainWindow, re);
login.Show();
}
protected override void OnStartup(StartupEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
base.OnStartup(e);
return;
}
string myMachineCode = MachineCodeHelper.GetDeviceMachineCode();
if (MachineCodeHelper.VerifyMachineCode(myMachineCode))
{
base.OnStartup(e);
}
else
{
MessageBox.Show("机器码不匹配!");
return;
}
}
protected override void RegisterRequiredTypes(IContainerRegistry containerRegistry)
{
base.RegisterRequiredTypes(containerRegistry);
//注册全局变量
containerRegistry.RegisterScoped<SystemConfig>();
containerRegistry.RegisterScoped<StepRunning>();
containerRegistry.RegisterScoped<ScopedContext>();
containerRegistry.RegisterScoped<DeviceManager>();
containerRegistry.RegisterSingleton<GlobalInfo>();
containerRegistry.RegisterSingleton<HardwareDataBroadcaster>();
containerRegistry.RegisterSingleton<CANSignalBroadcaster>();
containerRegistry.RegisterSingleton<GlobalConfig>();
//注册AutoMapper
var config = new MapperConfiguration(
cfg => cfg.AddProfile<AutoMapperProfile>(),
NullLoggerFactory.Instance
);
containerRegistry.RegisterSingleton<IMapper>(() => config.CreateMapper());
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//注册弹窗
containerRegistry.RegisterDialog<MessageBoxView, MessageBoxViewModel>("MessageBox");
// 注册通知管理器
INotificationManager NotificationManager = new NotificationManager();
containerRegistry.RegisterInstance<INotificationManager>(NotificationManager);
// 注册仓储
containerRegistry.RegisterScoped(typeof(SqlSugarRepository<>));
//注册服务
containerRegistry.Register<IMonitorValueService, MonitorValueService>();
containerRegistry.Register<ITestReportService, TestReportService>();
containerRegistry.Register<ITestCheckRecordService, TestCheckRecordService>();
}
//指定模块加载方式(需要手动将模块生成的dll放入Modules文件夹中)
protected override IModuleCatalog CreateModuleCatalog()
{
//指定模块加载方式为从文件夹中以反射发现并加载module(推荐用法)
return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
}
}
}