Files
ACP/ACP/App.xaml.cs
2026-07-30 14:06:33 +08:00

137 lines
5.4 KiB
C#

using Common;
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.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 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()
{
// 配置全局日志分发器:按 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>();
//注册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>();
}
//指定模块加载方式(需要手动将模块生成的dll放入Modules文件夹中)
protected override IModuleCatalog CreateModuleCatalog()
{
//指定模块加载方式为从文件夹中以反射发现并加载module(推荐用法)
return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
}
}
}