ShowVisionRender.xaml.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ImageFile;
  3. using Prism.Events;
  4. using Prism.Ioc;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Forms.Integration;
  14. using TeamAAS_VP.Core;
  15. using TeamAAS_VP.Enums;
  16. using TeamAAS_VP.Events;
  17. using TeamAAS_VP.Interfaces;
  18. using TeamAAS_VP.Models;
  19. using TeamAAS_VP.Resources.Languages;
  20. using TeamAAS_VP.ViewModels.Home;
  21. namespace TeamAAS_VP.Views.Home
  22. {
  23. /// <summary>
  24. /// Interaction logic for ShowVisionRender
  25. /// </summary>
  26. public partial class ShowVisionRender : UserControl
  27. {
  28. private ShowVisionRenderViewModel viewModel;
  29. // key=ShowRender.Id
  30. private readonly Dictionary<Guid, WindowsFormsHost> vmRenders;
  31. private readonly Dictionary<Guid, TextBlock> Titles;
  32. private readonly IEventAggregator _eventAggregator;
  33. private readonly ISystemDatabaseService _systemDatabaseService;
  34. private readonly IProductService _productService;
  35. // ======================= 【新增】保存任务控制:防闪退/防资源泄露 =======================
  36. private CancellationTokenSource _saveImageCts = new CancellationTokenSource();
  37. private readonly SemaphoreSlim _saveSemaphore = new SemaphoreSlim(2, 2);
  38. public ShowVisionRender()
  39. {
  40. InitializeComponent();
  41. vmRenders = new Dictionary<Guid, WindowsFormsHost>();
  42. Titles = new Dictionary<Guid, TextBlock>();
  43. viewModel = DataContext as ShowVisionRenderViewModel;
  44. _eventAggregator = viewModel._eventAggregator;
  45. _systemDatabaseService = ((Prism.PrismApplicationBase)App.Current).Container.Resolve<ISystemDatabaseService>();
  46. _productService = ((Prism.PrismApplicationBase)App.Current).Container.Resolve<IProductService>();
  47. viewModel.UpdateLayout += ViewModel_UpdateLayout;
  48. _eventAggregator.GetEvent<RenderUpdateNotification>().Subscribe(UpdateRenderModuleSource);
  49. _eventAggregator.GetEvent<ProductChangedNotification>().Subscribe(ProductChanged);
  50. // 【可选】控件卸载时取消所有保存任务,避免页面关闭后仍在截图/保存导致闪退
  51. this.Unloaded += ShowVisionRender_Unloaded;
  52. }
  53. private void ShowVisionRender_Unloaded(object sender, RoutedEventArgs e)
  54. {
  55. try
  56. {
  57. _saveImageCts.Cancel();
  58. _saveImageCts.Dispose();
  59. }
  60. catch { }
  61. }
  62. /// <summary>
  63. /// 更新某个工位/模块的显示内容(图像/图形/Record/保存)
  64. /// </summary>
  65. private void UpdateRenderModuleSource(ShowRender render)
  66. {
  67. this.gridRender.Dispatcher.BeginInvoke(new Action(() =>
  68. {
  69. foreach (var item in vmRenders)
  70. {
  71. if (item.Value?.Child is CogRecordDisplay disp)
  72. {
  73. // 通过 Tag(产品名) 匹配当前需要更新的窗口
  74. if (disp.Tag != null && disp.Tag.ToString() == render.ProceductName)
  75. {
  76. disp.Record = null;
  77. disp.Image = render.Image;
  78. disp.StaticGraphics.Clear();
  79. if (render.Graphic != null)
  80. disp.StaticGraphics.AddList(render.Graphic, "");
  81. if (render.Record != null)
  82. disp.Record = render.Record;
  83. // ======================= 【关键优化】保存图像走安全入口 =======================
  84. RequestSaveImage(render, disp);
  85. break;
  86. }
  87. }
  88. }
  89. }));
  90. }
  91. /// <summary>
  92. /// 【核心】根据 obj.Count 自动生成 1~16 个显示格子(标题+CogRecordDisplay)
  93. /// </summary>
  94. private async void ViewModel_UpdateLayout(System.Collections.ObjectModel.ObservableCollection<Models.ShowRender> obj)
  95. {
  96. // ======================= 【关键优化】布局刷新前先取消旧保存任务 =======================
  97. try
  98. {
  99. _saveImageCts.Cancel();
  100. _saveImageCts.Dispose();
  101. }
  102. catch { }
  103. _saveImageCts = new CancellationTokenSource();
  104. System.Windows.Media.FontFamily font = Application.Current.Resources["DefaultFont"] as System.Windows.Media.FontFamily;
  105. // 1) 清理旧控件
  106. if (vmRenders.Count > 0)
  107. {
  108. foreach (var render in vmRenders)
  109. {
  110. // WindowsFormsHost.Dispose() 会销毁 WinForms 句柄,防止资源泄漏
  111. render.Value?.Dispose();
  112. }
  113. }
  114. vmRenders.Clear();
  115. Titles.Clear();
  116. gridRender.Children.Clear();
  117. gridRender.RowDefinitions.Clear();
  118. gridRender.ColumnDefinitions.Clear();
  119. // 2) 判空
  120. if (obj == null || obj.Count == 0) return;
  121. // 3) 限制最大 16(防止越界)
  122. int count = Math.Min(obj.Count, 16);
  123. // 4) 计算行列(最大 4*4)
  124. GetGridSize(count, out int rows, out int cols);
  125. // 5) 创建主Grid的行列
  126. for (int r = 0; r < rows; r++)
  127. gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
  128. for (int c = 0; c < cols; c++)
  129. gridRender.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  130. gridRender.ShowGridLines = true;
  131. // 6) 逐个创建子格子(每个子格子两行:标题/画面)
  132. for (int i = 0; i < count; i++)
  133. {
  134. var sr = obj[i];
  135. // (1) 创建显示控件(WindowsFormsHost + CogRecordDisplay)
  136. var disp = new CogRecordDisplay()
  137. {
  138. Tag = sr.ProceductName,
  139. };
  140. //ConfigureCogDisplay(disp);
  141. var host = new WindowsFormsHost()
  142. {
  143. Tag = sr.ProceductName,
  144. Child = disp
  145. };
  146. vmRenders.Add(sr.Id, host);
  147. // (2) 创建标题
  148. var title = new TextBlock()
  149. {
  150. Text = sr.ProceductName,
  151. HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
  152. FontWeight = FontWeights.Bold,
  153. FontFamily = font,
  154. FontSize = 12,
  155. Foreground = System.Windows.Media.Brushes.Black
  156. };
  157. Titles.Add(sr.Id, title);
  158. // (3) 子Grid(标题 + 显示)
  159. var cell = new Grid();
  160. cell.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) }); // 标题
  161. cell.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); // 图像
  162. cell.Children.Add(title);
  163. cell.Children.Add(host);
  164. Grid.SetRow(title, 0);
  165. Grid.SetColumn(title, 0);
  166. Grid.SetRow(host, 1);
  167. Grid.SetColumn(host, 0);
  168. // (4) 放到主 gridRender 中
  169. int row = i / cols;
  170. int col = i % cols;
  171. gridRender.Children.Add(cell);
  172. Grid.SetRow(cell, row);
  173. Grid.SetColumn(cell, col);
  174. }
  175. }
  176. /// <summary>
  177. /// 根据数量返回最合适的 rows/cols(最大 4*4)
  178. /// </summary>
  179. private void GetGridSize(int count, out int rows, out int cols)
  180. {
  181. rows = 1;
  182. cols = 1;
  183. if (count <= 1) { rows = 1; cols = 1; return; }
  184. if (count <= 2) { rows = 1; cols = 2; return; }
  185. if (count <= 4) { rows = 2; cols = 2; return; }
  186. if (count <= 6) { rows = 2; cols = 3; return; }
  187. if (count <= 9) { rows = 3; cols = 3; return; }
  188. if (count <= 12) { rows = 3; cols = 4; return; }
  189. rows = 4;
  190. cols = 4;
  191. }
  192. /// <summary>
  193. /// 统一配置 VisionPro 显示属性
  194. /// </summary>
  195. private void ConfigureCogDisplay(CogRecordDisplay disp)
  196. {
  197. disp.HorizontalScrollBar = false;
  198. disp.VerticalScrollBar = false;
  199. disp.AutoFit = true;
  200. disp.BackColor = System.Drawing.SystemColors.ActiveCaption;
  201. }
  202. // ======================= 【新增】安全保存入口:解决闪退 =======================
  203. private void RequestSaveImage(ShowRender render, CogRecordDisplay disp)
  204. {
  205. if (render == null) return;
  206. if (disp == null) return;
  207. if (!render.IsSaveImage) return;
  208. // Original 模式不需要 CreateContentBitmap(你的 SaveImage 里 Original 只写 ICogImage)
  209. bool needRecordedBitmap =
  210. render.SaveImageModel == ProcedureSaveImageModel.Recorded ||
  211. render.SaveImageModel == ProcedureSaveImageModel.OriginalAndRecorded;
  212. _saveImageCts = new CancellationTokenSource();
  213. var token = _saveImageCts.Token;
  214. // 必须在控件的 UI 线程上执行 CreateContentBitmap
  215. disp.BeginInvoke(new Action(() =>
  216. {
  217. Bitmap clonedBitmap = null;
  218. try
  219. {
  220. token.ThrowIfCancellationRequested();
  221. if (needRecordedBitmap)
  222. {
  223. // UI线程创建
  224. Bitmap uiBitmap = (Bitmap)disp.CreateContentBitmap(
  225. Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Custom);
  226. // 立刻 Clone,后台线程只用 Clone(防止控件刷新/释放导致崩溃)
  227. clonedBitmap = (Bitmap)uiBitmap.Clone();
  228. // UI线程立刻释放
  229. uiBitmap.Dispose();
  230. }
  231. // 后台保存(限并发,防止GDI/内存瞬时飙升)
  232. Task.Run(async () =>
  233. {
  234. await _saveSemaphore.WaitAsync(token);
  235. try
  236. {
  237. token.ThrowIfCancellationRequested();
  238. SaveImage(
  239. render.SaveImageModel,
  240. render.SaveImagePathModel,
  241. render.SavePath,
  242. render.Image,
  243. clonedBitmap,
  244. render.Result,
  245. render.IsCompress
  246. );
  247. }
  248. catch (OperationCanceledException)
  249. {
  250. // 取消属于正常情况,不记录
  251. }
  252. catch (Exception ex)
  253. {
  254. LogHelper.WriteLogError("后台保存图像任务出错!", ex);
  255. }
  256. finally
  257. {
  258. // 确保释放 Clone 的 Bitmap,避免 GDI 泄漏导致闪退
  259. try { clonedBitmap?.Dispose(); } catch { }
  260. _saveSemaphore.Release();
  261. }
  262. }, token);
  263. }
  264. catch (OperationCanceledException)
  265. {
  266. try { clonedBitmap?.Dispose(); } catch { }
  267. }
  268. catch (Exception ex)
  269. {
  270. try { clonedBitmap?.Dispose(); } catch { }
  271. LogHelper.WriteLogError("RequestSaveImage(UI截图阶段) 出错!", ex);
  272. }
  273. }));
  274. }
  275. /// <summary>
  276. /// 保存图像(保持你原有逻辑不变,补充 Dispose 兜底,防止压缩分支不释放导致闪退)
  277. /// </summary>
  278. private void SaveImage(ProcedureSaveImageModel saveImageModel,
  279. ProcedureSaveImagePathModel saveImagePathModel,
  280. string path,
  281. ICogImage image,
  282. Bitmap reimage,
  283. bool result,
  284. bool isCompress)
  285. {
  286. if (string.IsNullOrEmpty(path) || string.IsNullOrWhiteSpace(path) || !Path.IsPathRooted(path))
  287. {
  288. _eventAggregator.GetEvent<TaskMessageNotification>()
  289. .Publish(new Models.MessageStruct() { Message = "保存图像路径为无效的路径!", level = MessageLevel.Alarm });
  290. return;
  291. }
  292. Task.Run(() =>
  293. {
  294. try
  295. {
  296. DateTime now = DateTime.Now;
  297. if (saveImageModel == ProcedureSaveImageModel.Original)
  298. {
  299. string Originalpath = $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  300. if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_OkNG_Time)
  301. {
  302. Originalpath = result
  303. ? $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{now:yyyy-MM-dd HH_mm_ss}.bmp"
  304. : $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  305. }
  306. else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ok_Time)
  307. {
  308. if (result)
  309. Originalpath = $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  310. else
  311. return;
  312. }
  313. else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ng_Time)
  314. {
  315. if (!result)
  316. Originalpath = $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  317. else
  318. return;
  319. }
  320. else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Formula_ProductSN_Time)
  321. {
  322. // 当前产品配方名(过滤非法字符)
  323. string formulaName = _productService.GetCurrentProduct().Name;
  324. foreach (char c in Path.GetInvalidFileNameChars())
  325. formulaName = formulaName.Replace(c, '_');
  326. // 产品SN(过滤非法字符)
  327. string productSN = _productService.CurrentProductCode;
  328. foreach (char c in Path.GetInvalidFileNameChars())
  329. productSN = productSN.Replace(c, '_');
  330. string resultStr = result ? "OK" : "NG";
  331. Originalpath = $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\{formulaName}\\{productSN}\\{productSN}-{now:yyyy-MM-dd HH_mm_ss}-{resultStr}.bmp";
  332. }
  333. if (!Directory.Exists(Path.GetDirectoryName(Originalpath)))
  334. Directory.CreateDirectory(Path.GetDirectoryName(Originalpath));
  335. using (CogImageFileBMP _cogImageFileTool = new CogImageFileBMP())
  336. {
  337. _cogImageFileTool.Open(Originalpath, CogImageFileModeConstants.Write);
  338. _cogImageFileTool.Append(image);
  339. _cogImageFileTool.Close();
  340. }
  341. }
  342. else if (saveImageModel == ProcedureSaveImageModel.Recorded)
  343. {
  344. string Recordedpath = $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  345. if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_OkNG_Time)
  346. {
  347. Recordedpath = result
  348. ? $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{now:yyyy-MM-dd HH_mm_ss}.bmp"
  349. : $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  350. }
  351. else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ok_Time)
  352. {
  353. if (result)
  354. Recordedpath = $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  355. else
  356. return;
  357. }
  358. else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ng_Time)
  359. {
  360. if (!result)
  361. Recordedpath = $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  362. else
  363. return;
  364. }
  365. else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Formula_ProductSN_Time)
  366. {
  367. string formulaName = _productService.GetCurrentProduct().Name;
  368. foreach (char c in Path.GetInvalidFileNameChars())
  369. formulaName = formulaName.Replace(c, '_');
  370. string productSN = _productService.CurrentProductCode;
  371. foreach (char c in Path.GetInvalidFileNameChars())
  372. productSN = productSN.Replace(c, '_');
  373. string resultStr = result ? "OK" : "NG";
  374. Recordedpath = $"{path}\\{now:yyyy-MM}\\{now:MM-dd}\\{formulaName}\\{productSN}\\{productSN}-{now:yyyy-MM-dd HH_mm_ss}-{resultStr}.bmp";
  375. }
  376. if (!Directory.Exists(Path.GetDirectoryName(Recordedpath)))
  377. Directory.CreateDirectory(Path.GetDirectoryName(Recordedpath));
  378. if (isCompress)
  379. {
  380. ImageHelper.CompressImage(reimage, Recordedpath, 100);
  381. }
  382. else
  383. {
  384. reimage.Save(Recordedpath);
  385. }
  386. }
  387. else if (saveImageModel == ProcedureSaveImageModel.OriginalAndRecorded)
  388. {
  389. string Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  390. string Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  391. if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_OkNG_Time)
  392. {
  393. if (result)
  394. {
  395. Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  396. Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  397. }
  398. else
  399. {
  400. Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  401. Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  402. }
  403. }
  404. else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ok_Time)
  405. {
  406. if (result)
  407. {
  408. Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  409. Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  410. }
  411. else
  412. {
  413. return;
  414. }
  415. }
  416. else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ng_Time)
  417. {
  418. if (!result)
  419. {
  420. Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  421. Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{now:yyyy-MM-dd HH_mm_ss}.bmp";
  422. }
  423. else
  424. {
  425. return;
  426. }
  427. }
  428. else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Formula_ProductSN_Time)
  429. {
  430. string formulaName = _productService.GetCurrentProduct().Name;
  431. foreach (char c in Path.GetInvalidFileNameChars())
  432. formulaName = formulaName.Replace(c, '_');
  433. string productSN = _productService.CurrentProductCode;
  434. foreach (char c in Path.GetInvalidFileNameChars())
  435. productSN = productSN.Replace(c, '_');
  436. string resultStr = result ? "OK" : "NG";
  437. Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\{formulaName}\\{productSN}\\{productSN}-{now:yyyy-MM-dd HH_mm_ss}-{resultStr}.bmp";
  438. Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\{formulaName}\\{productSN}\\{productSN}-{now:yyyy-MM-dd HH_mm_ss}-{resultStr}.bmp";
  439. }
  440. if (!Directory.Exists(Path.GetDirectoryName(Originalpath)))
  441. Directory.CreateDirectory(Path.GetDirectoryName(Originalpath));
  442. using (CogImageFileBMP _cogImageFileTool = new CogImageFileBMP())
  443. {
  444. _cogImageFileTool.Open(Originalpath, CogImageFileModeConstants.Write);
  445. _cogImageFileTool.Append(image);
  446. _cogImageFileTool.Close();
  447. }
  448. if (!Directory.Exists(Path.GetDirectoryName(Recordedpath)))
  449. Directory.CreateDirectory(Path.GetDirectoryName(Recordedpath));
  450. if (isCompress)
  451. {
  452. ImageHelper.CompressImage(reimage, Recordedpath, 100);
  453. }
  454. else
  455. {
  456. reimage.Save(Recordedpath);
  457. }
  458. }
  459. }
  460. catch (Exception ex)
  461. {
  462. LogHelper.WriteLogError("保存流程运行结果的图片时出错!", ex);
  463. }
  464. finally
  465. {
  466. // ======================= 【关键修复】无论是否压缩都释放Bitmap =======================
  467. try { reimage?.Dispose(); } catch { }
  468. }
  469. });
  470. }
  471. /// <summary>
  472. /// 切换产品时清空主页显示(保持你原有逻辑)
  473. /// </summary>
  474. private void ProductChanged(ProductModel product)
  475. {
  476. this.gridRender.Dispatcher.BeginInvoke(new Action(() =>
  477. {
  478. foreach (var item in vmRenders)
  479. {
  480. try
  481. {
  482. if (item.Value?.Child is CogRecordDisplay disp)
  483. {
  484. if (disp.Tag != null)
  485. {
  486. disp.Image = null;
  487. disp.StaticGraphics.Clear();
  488. // 你原来 break 只清一次;这里保留你的行为
  489. break;
  490. }
  491. }
  492. }
  493. catch (Exception ex)
  494. {
  495. LogHelper.WriteLogError(Lang.切换产品清除主页图像时出错, ex);
  496. }
  497. }
  498. }));
  499. }
  500. }
  501. }