aisop/src/SopHelper.App/App.xaml.cs
sophelper 5fb36a7569 fix(App): GUI 实操验证后的六处修复
实操跑了一遍软件,逐个修掉现场暴露的问题:
- 默认页导航:CreateShell 里立刻 RequestNavigate 会静默失败(区域未注册),挪到窗口 Loaded
- ComboBox:WPF 默认模板白底,深色主题下选中项看不见字,重写控件模板
- 相机下拉框:DisplayMemberPath 运行时没生效(显示 ToString()),改显式 ItemTemplate
- 命令状态:Prism DelegateCommand 不会因属性变化自动重估 CanExecute,
  选了相机后「开始/停止」仍灰着点不动——SelectedCamera/SelectedRecipe 里补 RaiseCanExecuteChanged
- 右下角「相机未连接」是静态文字,改成绑定 ConnectionText
- 手部模型默认路径与实际文件名不符(palm_detection_full_* / hand_landmark_sparse_*),
  之前 ResolveModel 返回 null、手部链路静默不跑,实测耗时从 0 变为 7.8ms
2026-11-03 03:35:04 +08:00

137 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.SopPage;
using SopHelper.VVMs.MainWindow;
namespace SopHelper;
/// <summary>
/// 应用程序入口,负责初始化 Prism 框架和注册组件(启动链照搬 plcsharp)
/// </summary>
public partial class App
{
/// <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 的构造注入解析不出来。
_ = containerRegistry.RegisterSingleton<ICameraService, UvcCameraService>();
_ = containerRegistry.RegisterSingleton<DetectionPipeline>();
_ = containerRegistry.RegisterSingleton<SopEngine>();
// ViewModel 工厂默认走容器解析,带构造注入的 ViewModel 必须先注册
_ = containerRegistry.Register<SopPageViewModel>();
}
/// <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;
}
/// <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
{
// 日志失败不影响主流程
}
}
}