From e55bd9348a37bed34fa2213f0941175c72fc9c83 Mon Sep 17 00:00:00 2001
From: sophelper <123211521@163.com>
Date: Tue, 3 Nov 2026 00:49:38 +0800
Subject: [PATCH] =?UTF-8?q?chore:=20=E5=B7=A5=E7=A8=8B=E9=AA=A8=E6=9E=B6?=
=?UTF-8?q?=20=E2=80=94=20Prism.DryIoc=20=E5=90=AF=E5=8A=A8=E9=93=BE(Attri?=
=?UTF-8?q?bute=20=E9=A9=B1=E5=8A=A8=E6=B3=A8=E5=86=8C/=E5=91=BD=E5=90=8D?=
=?UTF-8?q?=E7=BA=A6=E5=AE=9A=E7=BB=91=E5=AE=9A)=20+=20=E7=BA=AF=20WPF=20?=
=?UTF-8?q?=E6=A0=B7=E5=BC=8F=20+=20=E5=AF=BC=E8=88=AA=20shell?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 17 +++
README.md | 23 ++++
SopHelper.slnx | 6 +
models/.gitkeep | 0
src/SopHelper.App/App.xaml | 12 ++
src/SopHelper.App/App.xaml.cs | 119 ++++++++++++++++++
src/SopHelper.App/Core/Prism/CoreAttribute.cs | 39 ++++++
src/SopHelper.App/Core/Prism/MessageEvent.cs | 61 +++++++++
src/SopHelper.App/Core/Prism/ModelBase.cs | 48 +++++++
src/SopHelper.App/Core/Prism/NavigateItem.cs | 31 +++++
src/SopHelper.App/Core/Prism/ViewModelBase.cs | 17 +++
src/SopHelper.App/Models/NavigationModel.cs | 80 ++++++++++++
src/SopHelper.App/SopHelper.App.csproj | 36 ++++++
src/SopHelper.App/Themes/Styles.xaml | 108 ++++++++++++++++
.../VVMs/MainWindow/MainWindow.xaml | 66 ++++++++++
.../VVMs/MainWindow/MainWindow.xaml.cs | 17 +++
.../VVMs/MainWindow/MainWindowViewModel.cs | 49 ++++++++
src/SopHelper.App/VVMs/SopPage/SopPage.xaml | 14 +++
.../VVMs/SopPage/SopPage.xaml.cs | 18 +++
.../VVMs/SopPage/SopPageViewModel.cs | 20 +++
src/SopHelper.Engine/SopHelper.Engine.csproj | 22 ++++
yolo_models/.gitkeep | 0
22 files changed, 803 insertions(+)
create mode 100644 .gitignore
create mode 100644 README.md
create mode 100644 SopHelper.slnx
create mode 100644 models/.gitkeep
create mode 100644 src/SopHelper.App/App.xaml
create mode 100644 src/SopHelper.App/App.xaml.cs
create mode 100644 src/SopHelper.App/Core/Prism/CoreAttribute.cs
create mode 100644 src/SopHelper.App/Core/Prism/MessageEvent.cs
create mode 100644 src/SopHelper.App/Core/Prism/ModelBase.cs
create mode 100644 src/SopHelper.App/Core/Prism/NavigateItem.cs
create mode 100644 src/SopHelper.App/Core/Prism/ViewModelBase.cs
create mode 100644 src/SopHelper.App/Models/NavigationModel.cs
create mode 100644 src/SopHelper.App/SopHelper.App.csproj
create mode 100644 src/SopHelper.App/Themes/Styles.xaml
create mode 100644 src/SopHelper.App/VVMs/MainWindow/MainWindow.xaml
create mode 100644 src/SopHelper.App/VVMs/MainWindow/MainWindow.xaml.cs
create mode 100644 src/SopHelper.App/VVMs/MainWindow/MainWindowViewModel.cs
create mode 100644 src/SopHelper.App/VVMs/SopPage/SopPage.xaml
create mode 100644 src/SopHelper.App/VVMs/SopPage/SopPage.xaml.cs
create mode 100644 src/SopHelper.App/VVMs/SopPage/SopPageViewModel.cs
create mode 100644 src/SopHelper.Engine/SopHelper.Engine.csproj
create mode 100644 yolo_models/.gitkeep
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b9da61a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,17 @@
+# 构建产物
+bin/
+obj/
+
+# IDE
+.vs/
+.vscode/
+*.user
+
+# 模型文件(体积大,自行放置)
+models/*.onnx
+yolo_models/*.onnx
+
+# 运行期产物
+logs/
+snapshots/
+runs/
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..cfbc3f6
--- /dev/null
+++ b/README.md
@@ -0,0 +1,23 @@
+# SopHelper
+
+单 USB 相机的最小 SOP 辅助程序。
+
+- 架构照搬 `E:\10-Work\plcsharp`:Prism.DryIoc + Attribute 驱动注册 + 命名约定自动绑定。
+- 不含 HandyControl / SkiaSharp.Views.WPF,纯 WPF 样式。
+- 判定引擎移植自 `VisionInspectionSystem`(MIT),去掉多相机/海康/ONVIF/Modbus/用户权限。
+
+## 目录
+
+```
+src/SopHelper.Engine/ 纯类库:相机 · 推理 · SOP 引擎(无 UI 依赖)
+src/SopHelper.App/ Prism shell + SOP 页(WPF)
+configs/ SOP 配方(YAML)
+models/ MediaPipe 两个 onnx(不入 git)
+yolo_models/ YOLO onnx(不入 git)
+```
+
+## 构建
+
+```bash
+dotnet build SopHelper.sln
+```
diff --git a/SopHelper.slnx b/SopHelper.slnx
new file mode 100644
index 0000000..93846e4
--- /dev/null
+++ b/SopHelper.slnx
@@ -0,0 +1,6 @@
+
+
+
+
+
+
diff --git a/models/.gitkeep b/models/.gitkeep
new file mode 100644
index 0000000..e69de29
diff --git a/src/SopHelper.App/App.xaml b/src/SopHelper.App/App.xaml
new file mode 100644
index 0000000..1eef9b7
--- /dev/null
+++ b/src/SopHelper.App/App.xaml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
diff --git a/src/SopHelper.App/App.xaml.cs b/src/SopHelper.App/App.xaml.cs
new file mode 100644
index 0000000..58b6896
--- /dev/null
+++ b/src/SopHelper.App/App.xaml.cs
@@ -0,0 +1,119 @@
+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.Models;
+using SopHelper.VVMs.MainWindow;
+
+namespace SopHelper;
+
+///
+/// 应用程序入口,负责初始化 Prism 框架和注册组件(启动链照搬 plcsharp)
+///
+public partial class App
+{
+ ///
+ /// 注册应用程序所需的类型和服务
+ ///
+ protected override void RegisterTypes(IContainerRegistry containerRegistry)
+ {
+ RegisterEvents(); // 捕获全局异常
+
+ // 注册带 [Model] 特性的类,单例
+ foreach (var model in from t in Assembly.GetExecutingAssembly().GetTypes()
+ where t.GetCustomAttribute() is not null
+ select t)
+ {
+ _ = containerRegistry.RegisterSingleton(model);
+ }
+
+ // 注册带 [NavigationPage] 特性的 View 到导航
+ foreach (var view in from t in Assembly.GetExecutingAssembly().GetTypes()
+ where t.GetCustomAttribute() is not null
+ select t)
+ {
+ containerRegistry.RegisterForNavigation(view, view.Name);
+ }
+
+ _ = containerRegistry.RegisterSingleton();
+ }
+
+ ///
+ /// 创建Shell
+ ///
+ protected override Window CreateShell()
+ {
+ var navigate = Container.Resolve();
+
+ // 反射收集导航页
+ foreach (var view in from t in Assembly.GetExecutingAssembly().GetTypes()
+ where t.GetCustomAttribute() is not null
+ orderby t.GetCustomAttribute()!.Index
+ select t)
+ {
+ var page = view.GetCustomAttribute()!;
+
+ var item = Container.Resolve();
+ item.ViewName = page.ViewName;
+ item.DisplayName = page.DisplayName;
+ navigate.Pages.Add(item);
+ }
+
+ var window = Container.Resolve();
+
+ // 默认页
+ navigate.Navigate("SopPage");
+
+ return window;
+ }
+
+ ///
+ /// 自动寻找ViewModel并绑定到View,要求ViewModel命名为View的全名加上ViewModel,比如MainWindow的ViewModel命名为MainWindowViewModel
+ ///
+ 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);
+ });
+ }
+
+ ///
+ /// 捕获全局异常
+ ///
+ 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
+ {
+ // 日志失败不影响主流程
+ }
+ }
+}
diff --git a/src/SopHelper.App/Core/Prism/CoreAttribute.cs b/src/SopHelper.App/Core/Prism/CoreAttribute.cs
new file mode 100644
index 0000000..29ab00c
--- /dev/null
+++ b/src/SopHelper.App/Core/Prism/CoreAttribute.cs
@@ -0,0 +1,39 @@
+namespace SopHelper.Core.Prism;
+
+///
+/// 标志此特性的类会被注册为单例(照搬 plcsharp 约定)
+///
+[AttributeUsage(AttributeTargets.Class)]
+public class ModelAttribute : Attribute
+{
+}
+
+///
+/// 标志此特性的 View 会被注册到导航列表
+///
+[AttributeUsage(AttributeTargets.Class)]
+public class NavigationPageAttribute : Attribute
+{
+ ///
+ /// 视图名称(导航用)
+ ///
+ public string ViewName { get; set; } = "";
+
+ ///
+ /// Display名称
+ ///
+ public string DisplayName { get; set; } = "";
+
+ ///
+ /// 排序序号
+ ///
+ public int Index { get; set; }
+}
+
+///
+/// 标志此特性的类会被注册为弹出窗口
+///
+[AttributeUsage(AttributeTargets.Class)]
+public class DialogAttribute : Attribute
+{
+}
diff --git a/src/SopHelper.App/Core/Prism/MessageEvent.cs b/src/SopHelper.App/Core/Prism/MessageEvent.cs
new file mode 100644
index 0000000..b0b9370
--- /dev/null
+++ b/src/SopHelper.App/Core/Prism/MessageEvent.cs
@@ -0,0 +1,61 @@
+using Prism.Events;
+
+namespace SopHelper.Core.Prism;
+
+///
+/// 全局消息事件(照搬 plcsharp:模块间解耦都走事件聚合器)
+///
+public class MessageEvent : PubSubEvent
+{
+}
+
+///
+/// 消息内容
+///
+public struct Message
+{
+ ///
+ /// 消息目标
+ ///
+ public string Target { get; set; }
+
+ ///
+ /// 消息
+ ///
+ public string Content { get; set; }
+
+ ///
+ /// 消息类型 1 记录 2 弹出 3 弹出并记录
+ ///
+ public ShowType Type { get; set; }
+
+ ///
+ /// 级别
+ ///
+ public MsgLevel Level { get; set; }
+}
+
+///
+/// 显示方式
+///
+public enum ShowType
+{
+ /// 仅记录日志
+ Log = 1,
+
+ /// 仅提示
+ Show = 2,
+
+ /// 提示并记录
+ Both = 3,
+}
+
+///
+/// 消息级别
+///
+public enum MsgLevel
+{
+ Info,
+ Warn,
+ Error,
+}
diff --git a/src/SopHelper.App/Core/Prism/ModelBase.cs b/src/SopHelper.App/Core/Prism/ModelBase.cs
new file mode 100644
index 0000000..1562226
--- /dev/null
+++ b/src/SopHelper.App/Core/Prism/ModelBase.cs
@@ -0,0 +1,48 @@
+using Prism.Events;
+using Prism.Ioc;
+using Prism.Mvvm;
+
+namespace SopHelper.Core.Prism;
+
+///
+/// 模型Base
+///
+public class ModelBase : BindableBase
+{
+ ///
+ /// 事件聚合器
+ ///
+ protected readonly IEventAggregator _eventAggregator;
+
+ ///
+ /// DI 容器
+ ///
+ protected readonly IContainerExtension _container;
+
+ ///
+ /// 模型Base
+ ///
+ public ModelBase(IContainerExtension container, IEventAggregator ea)
+ {
+ _container = container;
+ _eventAggregator = ea;
+
+ _eventAggregator.GetEvent().Subscribe(OnMessage, ThreadOption.PublisherThread, true);
+
+ AppDomain.CurrentDomain.ProcessExit += OnExit;
+ }
+
+ ///
+ /// 收到消息(子类覆写做本模块的落库/统计)
+ ///
+ protected virtual void OnMessage(Message message)
+ {
+ }
+
+ ///
+ /// 进程退出(子类覆写做释放)
+ ///
+ protected virtual void OnExit(object? sender, EventArgs e)
+ {
+ }
+}
diff --git a/src/SopHelper.App/Core/Prism/NavigateItem.cs b/src/SopHelper.App/Core/Prism/NavigateItem.cs
new file mode 100644
index 0000000..4af4c70
--- /dev/null
+++ b/src/SopHelper.App/Core/Prism/NavigateItem.cs
@@ -0,0 +1,31 @@
+using Prism.Mvvm;
+
+namespace SopHelper.Core.Prism;
+
+///
+/// Navigate项
+///
+public class NavigateItem : BindableBase
+{
+ private string _viewName = "";
+
+ ///
+ /// 视图名称
+ ///
+ public string ViewName
+ {
+ get { return _viewName; }
+ set { SetProperty(ref _viewName, value); }
+ }
+
+ private string _displayName = "";
+
+ ///
+ /// Display名称
+ ///
+ public string DisplayName
+ {
+ get { return _displayName; }
+ set { SetProperty(ref _displayName, value); }
+ }
+}
diff --git a/src/SopHelper.App/Core/Prism/ViewModelBase.cs b/src/SopHelper.App/Core/Prism/ViewModelBase.cs
new file mode 100644
index 0000000..40777ed
--- /dev/null
+++ b/src/SopHelper.App/Core/Prism/ViewModelBase.cs
@@ -0,0 +1,17 @@
+using Prism.Mvvm;
+using Prism.Navigation;
+
+namespace SopHelper.Core.Prism;
+
+///
+/// 视图模型Base
+///
+public abstract class ViewModelBase : BindableBase, IDestructible
+{
+ ///
+ /// Destroy
+ ///
+ public virtual void Destroy()
+ {
+ }
+}
diff --git a/src/SopHelper.App/Models/NavigationModel.cs b/src/SopHelper.App/Models/NavigationModel.cs
new file mode 100644
index 0000000..06027e9
--- /dev/null
+++ b/src/SopHelper.App/Models/NavigationModel.cs
@@ -0,0 +1,80 @@
+using System.Collections.ObjectModel;
+using Prism.Events;
+using Prism.Ioc;
+using Prism.Navigation.Regions;
+using SopHelper.Core.Prism;
+
+namespace SopHelper.Models;
+
+///
+/// Navigate模型
+///
+[Model]
+public class NavigationModel : ModelBase
+{
+ private readonly IRegionManager _regionManager;
+
+ ///
+ /// Navigate模型
+ ///
+ public NavigationModel(IContainerExtension container, IEventAggregator ea, IRegionManager regionManager)
+ : base(container, ea)
+ {
+ _regionManager = regionManager;
+ }
+
+ private ObservableCollection _pages = [];
+
+ ///
+ /// 导航页列表(shell 侧栏绑定)
+ ///
+ public ObservableCollection Pages
+ {
+ get { return _pages; }
+ set { SetProperty(ref _pages, value); }
+ }
+
+ private NavigateItem? _target;
+
+ ///
+ /// 当前页
+ ///
+ public NavigateItem? Target
+ {
+ get { return _target; }
+ set { SetProperty(ref _target, value); }
+ }
+
+ private int _selectedIndex = -1;
+
+ ///
+ /// 侧栏选中索引
+ ///
+ public int SelectedIndex
+ {
+ get { return _selectedIndex; }
+ set
+ {
+ if (!SetProperty(ref _selectedIndex, value)) return;
+ if (value >= 0 && value < Pages.Count) Navigate(Pages[value].ViewName);
+ }
+ }
+
+ ///
+ /// 导航到指定页
+ ///
+ public void Navigate(string viewName)
+ {
+ if (string.IsNullOrEmpty(viewName)) return;
+
+ Target = Pages.FirstOrDefault(n => n.ViewName == viewName);
+ if (Target is null) return;
+
+ _regionManager.RequestNavigate(ContentRegion, viewName);
+ }
+
+ ///
+ /// 内容区名(shell 与导航的唯一约定)
+ ///
+ public const string ContentRegion = "ContentRegion";
+}
diff --git a/src/SopHelper.App/SopHelper.App.csproj b/src/SopHelper.App/SopHelper.App.csproj
new file mode 100644
index 0000000..d86eaa5
--- /dev/null
+++ b/src/SopHelper.App/SopHelper.App.csproj
@@ -0,0 +1,36 @@
+
+
+
+ WinExe
+ net8.0-windows
+ true
+ latest
+ enable
+ enable
+ x64
+ AnyCPU;x64
+ true
+ SopHelper
+ SopHelper
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/SopHelper.App/Themes/Styles.xaml b/src/SopHelper.App/Themes/Styles.xaml
new file mode 100644
index 0000000..54e9089
--- /dev/null
+++ b/src/SopHelper.App/Themes/Styles.xaml
@@ -0,0 +1,108 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/SopHelper.App/VVMs/MainWindow/MainWindow.xaml b/src/SopHelper.App/VVMs/MainWindow/MainWindow.xaml
new file mode 100644
index 0000000..7c7259e
--- /dev/null
+++ b/src/SopHelper.App/VVMs/MainWindow/MainWindow.xaml
@@ -0,0 +1,66 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/SopHelper.App/VVMs/MainWindow/MainWindow.xaml.cs b/src/SopHelper.App/VVMs/MainWindow/MainWindow.xaml.cs
new file mode 100644
index 0000000..3a6e9bd
--- /dev/null
+++ b/src/SopHelper.App/VVMs/MainWindow/MainWindow.xaml.cs
@@ -0,0 +1,17 @@
+using System.Windows;
+
+namespace SopHelper.VVMs.MainWindow;
+
+///
+/// 主窗口(shell:侧导航 + 内容区)
+///
+public partial class MainWindow : Window
+{
+ ///
+ /// 主窗口
+ ///
+ public MainWindow()
+ {
+ InitializeComponent();
+ }
+}
diff --git a/src/SopHelper.App/VVMs/MainWindow/MainWindowViewModel.cs b/src/SopHelper.App/VVMs/MainWindow/MainWindowViewModel.cs
new file mode 100644
index 0000000..b297603
--- /dev/null
+++ b/src/SopHelper.App/VVMs/MainWindow/MainWindowViewModel.cs
@@ -0,0 +1,49 @@
+using Prism.Commands;
+using Prism.Mvvm;
+using SopHelper.Models;
+
+namespace SopHelper.VVMs.MainWindow;
+
+///
+/// Main窗口视图模型
+///
+public class MainWindowViewModel : BindableBase
+{
+ private readonly NavigationModel _navigation;
+
+ ///
+ /// Main窗口视图模型
+ ///
+ public MainWindowViewModel(NavigationModel navigation)
+ {
+ _navigation = navigation;
+ }
+
+ private string _title = "SopHelper";
+
+ ///
+ /// 标题
+ ///
+ public string Title
+ {
+ get { return _title; }
+ set { SetProperty(ref _title, value); }
+ }
+
+ ///
+ /// 导航模型(侧栏绑定)
+ ///
+ public NavigationModel Navigation => _navigation;
+
+ private DelegateCommand? _exitCommand;
+
+ ///
+ /// 退出
+ ///
+ public DelegateCommand ExitCommand => _exitCommand ??= new DelegateCommand(OnExit);
+
+ private void OnExit()
+ {
+ System.Windows.Application.Current.Shutdown();
+ }
+}
diff --git a/src/SopHelper.App/VVMs/SopPage/SopPage.xaml b/src/SopHelper.App/VVMs/SopPage/SopPage.xaml
new file mode 100644
index 0000000..96a901b
--- /dev/null
+++ b/src/SopHelper.App/VVMs/SopPage/SopPage.xaml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
diff --git a/src/SopHelper.App/VVMs/SopPage/SopPage.xaml.cs b/src/SopHelper.App/VVMs/SopPage/SopPage.xaml.cs
new file mode 100644
index 0000000..053b06b
--- /dev/null
+++ b/src/SopHelper.App/VVMs/SopPage/SopPage.xaml.cs
@@ -0,0 +1,18 @@
+using SopHelper.Core.Prism;
+
+namespace SopHelper.VVMs.SopPage;
+
+///
+/// SOP 主页(画面 + 步骤卡 + 参数)
+///
+[NavigationPage(ViewName = "SopPage", DisplayName = "SOP 辅助", Index = 0)]
+public partial class SopPage : System.Windows.Controls.UserControl
+{
+ ///
+ /// SOP 主页
+ ///
+ public SopPage()
+ {
+ InitializeComponent();
+ }
+}
diff --git a/src/SopHelper.App/VVMs/SopPage/SopPageViewModel.cs b/src/SopHelper.App/VVMs/SopPage/SopPageViewModel.cs
new file mode 100644
index 0000000..5e29b1b
--- /dev/null
+++ b/src/SopHelper.App/VVMs/SopPage/SopPageViewModel.cs
@@ -0,0 +1,20 @@
+using Prism.Mvvm;
+
+namespace SopHelper.VVMs.SopPage;
+
+///
+/// SOP 主页视图模型(占位,画面与步骤卡在 s5 填充)
+///
+public class SopPageViewModel : BindableBase
+{
+ private string _status = "骨架已就绪(s2)";
+
+ ///
+ /// 状态文本
+ ///
+ public string Status
+ {
+ get { return _status; }
+ set { SetProperty(ref _status, value); }
+ }
+}
diff --git a/src/SopHelper.Engine/SopHelper.Engine.csproj b/src/SopHelper.Engine/SopHelper.Engine.csproj
new file mode 100644
index 0000000..e27fe90
--- /dev/null
+++ b/src/SopHelper.Engine/SopHelper.Engine.csproj
@@ -0,0 +1,22 @@
+
+
+
+
+ net8.0
+ latest
+ enable
+ enable
+ x64
+ AnyCPU;x64
+ true
+ SopHelper.Engine
+ SopHelper.Engine
+
+
+
+
+
+
+
+
+
diff --git a/yolo_models/.gitkeep b/yolo_models/.gitkeep
new file mode 100644
index 0000000..e69de29