From 6397a33644429372de79e9a93892ff5a12d20da9 Mon Sep 17 00:00:00 2001
From: sophelper <123211521@163.com>
Date: Fri, 6 Nov 2026 00:01:02 +0800
Subject: [PATCH] Add ONNX model file picker to recipe editor
---
.../VVMs/RecipeEditor/RecipeEditor.xaml | 7 ++--
.../RecipeEditor/RecipeEditorViewModel.cs | 34 +++++++++++++++++++
2 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditor.xaml b/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditor.xaml
index 9950205..7f4e091 100644
--- a/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditor.xaml
+++ b/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditor.xaml
@@ -533,10 +533,13 @@
+
+
@@ -558,8 +561,8 @@
-
+
diff --git a/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditorViewModel.cs b/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditorViewModel.cs
index 3a69aef..ad239e0 100644
--- a/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditorViewModel.cs
+++ b/src/SopHelper.App/VVMs/RecipeEditor/RecipeEditorViewModel.cs
@@ -44,12 +44,14 @@ public class RecipeEditorViewModel : BindableBase, IDialogAware
if (!SetProperty(ref _isCapturing, value)) return;
CaptureImageCommand.RaiseCanExecuteChanged();
OpenImageCommand.RaiseCanExecuteChanged();
+ BrowseModelCommand.RaiseCanExecuteChanged();
SaveCommand.RaiseCanExecuteChanged();
CloseCommand.RaiseCanExecuteChanged();
}
}
public DelegateCommand CaptureImageCommand { get; }
public DelegateCommand OpenImageCommand { get; }
+ public DelegateCommand BrowseModelCommand { get; }
/// 当前编辑的配方目录(configs/sop)
private string _directory = "";
@@ -94,6 +96,7 @@ public class RecipeEditorViewModel : BindableBase, IDialogAware
CaptureImageCommand = new DelegateCommand(async () => await CaptureImageAsync(),
() => !IsCapturing && _camera is not null && _cameraInfo is not null);
OpenImageCommand = new DelegateCommand(OpenImage, () => !IsCapturing);
+ BrowseModelCommand = new DelegateCommand(BrowseModel, () => !IsCapturing);
}
///
@@ -159,6 +162,37 @@ public class RecipeEditorViewModel : BindableBase, IDialogAware
catch (Exception ex) { PreviewStatus = "打开图片失败:" + ex.Message; }
}
+ private void BrowseModel()
+ {
+ if (Recipe.Model is null) return;
+ var dialog = new OpenFileDialog
+ {
+ Title = "选择 ONNX 检测模型",
+ Filter = "ONNX 模型 (*.onnx)|*.onnx",
+ DefaultExt = ".onnx",
+ CheckFileExists = true,
+ Multiselect = false,
+ InitialDirectory = AppContext.BaseDirectory,
+ };
+ try
+ {
+ if (!string.IsNullOrWhiteSpace(Recipe.Model.Path))
+ {
+ var currentPath = Path.GetFullPath(Recipe.Model.Path, AppContext.BaseDirectory);
+ var directory = Path.GetDirectoryName(currentPath);
+ if (Directory.Exists(directory)) dialog.InitialDirectory = directory;
+ if (File.Exists(currentPath)) dialog.FileName = Path.GetFileName(currentPath);
+ }
+ }
+ catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
+ {
+ // 手填路径无效时,仍可从程序目录重新选择。
+ }
+ if (dialog.ShowDialog() != true) return;
+ Recipe.Model.Path = dialog.FileName;
+ RefreshWarnings();
+ }
+
private async Task CaptureImageAsync()
{
if (IsCapturing || _camera is null || _cameraInfo is null) return;