commit e55bd9348a37bed34fa2213f0941175c72fc9c83 Author: sophelper <123211521@163.com> Date: Tue Nov 3 00:49:38 2026 +0800 chore: 工程骨架 — Prism.DryIoc 启动链(Attribute 驱动注册/命名约定绑定) + 纯 WPF 样式 + 导航 shell 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 @@ + + + + + + + + + + + + + + + +