168 lines
6.0 KiB
C#
168 lines
6.0 KiB
C#
using System.IO;
|
||
using System.Reflection;
|
||
using System.Windows;
|
||
using System.Windows.Threading;
|
||
using Prism.Ioc;
|
||
using Prism.Mvvm;
|
||
using SopHelper.Core.Prism;
|
||
using SopHelper.Engine.Camera;
|
||
using SopHelper.Engine.Pipeline;
|
||
using SopHelper.Engine.Sop;
|
||
using SopHelper.Models;
|
||
using SopHelper.VVMs.RecipeEditor;
|
||
using SopHelper.VVMs.SopPage;
|
||
using SopHelper.VVMs.MainWindow;
|
||
|
||
namespace SopHelper;
|
||
|
||
/// <summary>
|
||
/// 应用程序入口,负责初始化 Prism 框架和注册组件(启动链照搬 plcsharp)
|
||
/// </summary>
|
||
public partial class App
|
||
{
|
||
private ICameraService? _camera;
|
||
private DetectionPipeline? _pipeline;
|
||
private Engine.Training.WorkerClient? _trainingWorker;
|
||
|
||
/// <summary>
|
||
/// 注册应用程序所需的类型和服务
|
||
/// </summary>
|
||
protected override void RegisterTypes(IContainerRegistry containerRegistry)
|
||
{
|
||
RegisterEvents(); // 捕获全局异常
|
||
|
||
// 注册带 [Model] 特性的类,单例
|
||
foreach (var model in from t in Assembly.GetExecutingAssembly().GetTypes()
|
||
where t.GetCustomAttribute<ModelAttribute>() is not null
|
||
select t)
|
||
{
|
||
_ = containerRegistry.RegisterSingleton(model);
|
||
}
|
||
|
||
// 注册带 [NavigationPage] 特性的 View 到导航
|
||
foreach (var view in from t in Assembly.GetExecutingAssembly().GetTypes()
|
||
where t.GetCustomAttribute<NavigationPageAttribute>() is not null
|
||
select t)
|
||
{
|
||
containerRegistry.RegisterForNavigation(view, view.Name);
|
||
}
|
||
|
||
_ = containerRegistry.RegisterSingleton<NavigationModel>();
|
||
|
||
// 引擎层三件套。它们在 SopHelper.Engine 程序集里,上面的 [Model] 反射扫描只覆盖本程序集,
|
||
// 所以必须显式注册,否则 SOP 页 ViewModel 的构造注入解析不出来。
|
||
// 共享硬件和推理由应用持有;页面销毁和容器回收不能代替应用退出清理。
|
||
_camera = new CameraService();
|
||
_pipeline = new DetectionPipeline(_camera);
|
||
containerRegistry.RegisterInstance<ICameraService>(_camera);
|
||
containerRegistry.RegisterInstance(_pipeline);
|
||
_ = containerRegistry.RegisterSingleton<SopEngine>();
|
||
|
||
// ViewModel 工厂默认走容器解析,带构造注入的 ViewModel 必须先注册
|
||
_ = containerRegistry.RegisterSingleton<SopPageViewModel>();
|
||
_trainingWorker = new Engine.Training.WorkerClient();
|
||
containerRegistry.RegisterInstance(_trainingWorker);
|
||
containerRegistry.RegisterSingleton<Training.MainViewModel>();
|
||
|
||
// 配方编辑器弹窗(SOP 页「编辑」「新建」按钮弹出)
|
||
containerRegistry.RegisterDialog<RecipeEditor, RecipeEditorViewModel>("RecipeEditorDialog");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建Shell
|
||
/// </summary>
|
||
protected override Window CreateShell()
|
||
{
|
||
var navigate = Container.Resolve<NavigationModel>();
|
||
|
||
// 反射收集导航页
|
||
foreach (var view in from t in Assembly.GetExecutingAssembly().GetTypes()
|
||
where t.GetCustomAttribute<NavigationPageAttribute>() is not null
|
||
orderby t.GetCustomAttribute<NavigationPageAttribute>()!.Index
|
||
select t)
|
||
{
|
||
var page = view.GetCustomAttribute<NavigationPageAttribute>()!;
|
||
|
||
var item = Container.Resolve<NavigateItem>();
|
||
item.ViewName = page.ViewName;
|
||
item.DisplayName = page.DisplayName;
|
||
navigate.Pages.Add(item);
|
||
}
|
||
|
||
var window = Container.Resolve<MainWindow>();
|
||
|
||
// 默认页:区域要等窗口加载后才注册,CreateShell 里立刻 RequestNavigate 会静默失败
|
||
//(实测启动后内容区是空的),必须等 Loaded;走 SelectedIndex 让侧栏同步高亮。
|
||
window.Loaded += (_, _) =>
|
||
{
|
||
if (navigate.Pages.Count > 0) navigate.SelectedIndex = 0;
|
||
};
|
||
|
||
return window;
|
||
}
|
||
|
||
protected override void OnExit(ExitEventArgs e)
|
||
{
|
||
try
|
||
{
|
||
_trainingWorker?.Dispose();
|
||
// Dispose 等待采集线程退出、归还缓冲并关闭设备,然后才能释放推理模型。
|
||
try { _camera?.Dispose(); }
|
||
catch (Exception ex) { Log(ex, "Exit.Camera"); }
|
||
try { _pipeline?.Dispose(); }
|
||
catch (Exception ex) { Log(ex, "Exit.Pipeline"); }
|
||
}
|
||
finally
|
||
{
|
||
base.OnExit(e);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 自动寻找ViewModel并绑定到View,要求ViewModel命名为View的全名加上ViewModel,比如MainWindow的ViewModel命名为MainWindowViewModel
|
||
/// </summary>
|
||
protected override void ConfigureViewModelLocator()
|
||
{
|
||
base.ConfigureViewModelLocator();
|
||
|
||
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
|
||
{
|
||
var viewName = viewType.FullName;
|
||
var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
|
||
var viewModelName = $"{viewName}ViewModel, {viewAssemblyName}";
|
||
return Type.GetType(viewModelName);
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 捕获全局异常
|
||
/// </summary>
|
||
private static void RegisterEvents()
|
||
{
|
||
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
|
||
Log(e.ExceptionObject as Exception, "AppDomain");
|
||
|
||
Current.DispatcherUnhandledException += (_, e) =>
|
||
{
|
||
Log(e.Exception, "Dispatcher");
|
||
e.Handled = true; // 上位机不能因一次异常退出
|
||
};
|
||
}
|
||
|
||
private static void Log(Exception? ex, string from)
|
||
{
|
||
if (ex is null) return;
|
||
var line = $"[{DateTime.Now:HH:mm:ss.fff}] [{from}] {ex.Message}";
|
||
Console.WriteLine(line);
|
||
try
|
||
{
|
||
Directory.CreateDirectory("logs");
|
||
File.AppendAllText(Path.Combine("logs", "error.log"), line + Environment.NewLine);
|
||
}
|
||
catch
|
||
{
|
||
// 日志失败不影响主流程
|
||
}
|
||
}
|
||
}
|