UCDispVm.xaml.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. namespace Plugins.Vm.Views
  6. {
  7. /// <summary>
  8. /// VM 主页多画面显示控件(镜像 UCDispVpp 的角色):按 RowColNumber 铺设画面框网格。
  9. /// 单例,供 VisionMasterEngine 与 VM 图像显示插件访问。
  10. /// </summary>
  11. public partial class UCDispVm : UserControl
  12. {
  13. private static readonly Lazy<UCDispVm> _instance = new Lazy<UCDispVm>(() => new UCDispVm());
  14. public static UCDispVm Instance => _instance.Value;
  15. /// <summary>当前画面框集合(下标 = 画面序号-1)。</summary>
  16. public List<VmWindow> vmWindow { get; } = new List<VmWindow>();
  17. public UCDispVm()
  18. {
  19. InitializeComponent();
  20. }
  21. private int[] _rowCol = new[] { 0, 1 };
  22. /// <summary>{画面总数, 排列方式}:arrange 1=水平最优行列,2=垂直最优行列;总数0=清空。</summary>
  23. public int[] RowColNumber
  24. {
  25. get => _rowCol;
  26. set
  27. {
  28. _rowCol = value ?? new[] { 0, 1 };
  29. RebuildFrames();
  30. }
  31. }
  32. private void RebuildFrames()
  33. {
  34. if (!Dispatcher.CheckAccess()) { Dispatcher.Invoke(new Action(RebuildFrames)); return; }
  35. RootGrid.Children.Clear();
  36. RootGrid.RowDefinitions.Clear();
  37. RootGrid.ColumnDefinitions.Clear();
  38. vmWindow.Clear();
  39. int count = _rowCol.Length > 0 ? _rowCol[0] : 0;
  40. int arrange = _rowCol.Length > 1 ? _rowCol[1] : 1;
  41. if (count <= 0) return;
  42. int cols = (int)Math.Ceiling(Math.Sqrt(count));
  43. if (cols < 1) cols = 1;
  44. if (arrange == 2)
  45. {
  46. int rows = (int)Math.Ceiling(Math.Sqrt(count));
  47. if (rows < 1) rows = 1;
  48. cols = (int)Math.Ceiling((double)count / rows);
  49. if (cols < 1) cols = 1;
  50. }
  51. int rowCount = (count + cols - 1) / cols;
  52. for (int c = 0; c < cols; c++) RootGrid.ColumnDefinitions.Add(new ColumnDefinition());
  53. for (int r = 0; r < rowCount; r++) RootGrid.RowDefinitions.Add(new RowDefinition());
  54. for (int i = 0; i < count; i++)
  55. {
  56. var w = new VmWindow();
  57. Grid.SetRow(w, i / cols);
  58. Grid.SetColumn(w, i % cols);
  59. RootGrid.Children.Add(w);
  60. vmWindow.Add(w);
  61. }
  62. }
  63. }
  64. }