修复首次打开配方时区域下拉框选中值为空

This commit is contained in:
tfarcraw 2026-11-20 21:43:14 +08:00
parent 0b377d2afa
commit af887f88ba
2 changed files with 28 additions and 13 deletions

View File

@ -158,7 +158,12 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware
// 新建时文件名还没定,用配方名兜底;已存在的文件名不可改(SOP 页按文件名选配方)
var fileName = string.IsNullOrEmpty(_path) ? config.Sop.Name : Path.GetFileNameWithoutExtension(_path);
Recipe = RecipeNode.From(config, fileName, _isNew);
var recipe = RecipeNode.From(config, fileName, _isNew);
var selectedStep = recipe.SelectedStep;
// 先发布配方的下拉选项,再绑定步骤,避免区域选中值先于 ItemsSource 到达而被清空。
recipe.SelectedStep = null;
Recipe = recipe;
recipe.SelectedStep = selectedStep;
Recipe.Changed += RefreshWarnings;
InitializeRecipeTools();
LoadAgentSettings();

View File

@ -173,7 +173,7 @@ internal static class Smoke
if(Environment.GetCommandLineArgs().Contains("--recipe-close-only"))
{ await CheckRecipeClose(app,lifetime);_exit=0;return; }
if(Environment.GetCommandLineArgs().Contains("--step-trigger-tabs-only"))
{ await CheckStepTriggerTabs();_exit=0;return; }
{ await CheckStepTriggerTabs(app,lifetime);_exit=0;return; }
if(Environment.GetCommandLineArgs().Contains("--runtime-settings-only"))
{
var model=app.Container.Resolve<SopPageViewModel>();
@ -1425,24 +1425,34 @@ internal static class Smoke
Check(owner.IsVisible&&buttons.Length==2,"关闭保存提示仅有保存并关闭和不保存两个按钮");
buttons.Single(b=>b.Name==buttonName).RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));
}
private static async Task CheckStepTriggerTabs()
private static async Task CheckStepTriggerTabs(App app,IClassicDesktopStyleApplicationLifetime lifetime)
{
var model=new RecipeEditorViewModel();
model.Recipe.AddRegion("work");model.Recipe.AddRegion("finish");
var first=model.Recipe.AddStep();first.Detection.Method="hand_in_region";
first.Detection.Region="work";first.Detection.FromRegion="work";first.Detection.ToRegion="finish";
first.Detection.Hand="left";first.AiImageMode="fixed_region";first.AiImageRegion="work";
var second=model.Recipe.AddStep();second.Detection.Method="time_elapsed";
model.Recipe.SelectedStep=first;
var editor=new RecipeEditor{DataContext=model};
var dialog=new Window{Content=editor,Width=1040,Height=900};dialog.Show();await Task.Delay(60);
var recipe=new RecipeNode();
recipe.AddRegion("work");recipe.AddRegion("finish");
var step=recipe.AddStep();step.Detection.Method="hand_in_region";
step.Detection.Region="work";step.Detection.FromRegion="work";step.Detection.ToRegion="finish";
step.Detection.Hand="left";step.AiImageMode="fixed_region";step.AiImageRegion="work";
recipe.AddStep().Detection.Method="time_elapsed";
var config=recipe.ToConfig();config.Sop.Name="区域初始选择";
app.Container.Resolve<IDialogService>().ShowDialog("RecipeEditorDialog",new DialogParameters
{{"directory",new RecipeWorkspace(Environment.GetEnvironmentVariable("SOPHELPER_WORKSPACE_ROOT")!).RecipesDirectory},
{"config",config},{"new",true}},_=>{});
await Task.Delay(60);
var dialog=lifetime.Windows.OfType<RecipeDialogWindow>().Single();
var editor=dialog.GetVisualDescendants().OfType<RecipeEditor>().Single();
var model=(RecipeEditorViewModel)editor.DataContext!;
var first=model.Recipe.Steps[0];var second=model.Recipe.Steps[1];
try
{
var tabs=editor.GetVisualDescendants().OfType<TabControl>().First();
var picker=editor.FindControl<ComboBox>("StepTriggerMethodPicker")!;
var selections=editor.FindControl<ScrollViewer>("StepParameters")!.GetVisualDescendants()
.OfType<ComboBox>().Select(c=>(Picker:c,Selected:c.SelectedItem)).ToArray();
Check(selections.Length==7&&selections.All(s=>s.Selected is not null),"Step selectors initially display all configured values");
Check(selections.Select(s=>s.Selected?.ToString()).SequenceEqual(
new[]{"hand_in_region","work","work","finish","left","fixed_region","work"}),
"Step selectors initially display all configured values");
Check(editor.FindControl<Grid>("TriggerRegionRow")!.GetVisualDescendants().OfType<ComboBox>().Single().SelectedItem?.ToString()=="work",
"Opening a recipe through Prism immediately displays the configured trigger region");
void CheckSelection(string expected)
{
Check(picker.SelectedItem?.ToString()==expected,"Trigger selection survives tab changes: "+expected);