MainWindow.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Windows;
  2. using TeamAAS.Camera;
  3. using TeamAAS.Dialogs;
  4. using TeamAAS.ViewModels;
  5. namespace TeamAAS
  6. {
  7. public partial class MainWindow
  8. {
  9. // 【调试开关】主页右上角“ResultRegistry 监控”按钮:调试期开启;生产发布前置为 false 即可隐藏入口。
  10. private const bool EnableRegistryMonitorButton = true;
  11. // 注册表监控窗口(非模式、单实例);关闭后置空,允许再次打开
  12. private Views.RegistryDebugWindow _registryMonitorWindow;
  13. public MainWindow(MainWindowViewModel viewModel)
  14. {
  15. InitializeComponent();
  16. DataContext = viewModel;
  17. Loaded += MainWindow_Loaded;
  18. Closing += MainWindow_Closing;
  19. // 分辨率/DPI 自适应:主窗口计算全局缩放因子(1600x900 基准),所有弹窗跟随
  20. UiScaler.AttachMain(this);
  21. // 调试用:注册表监控按钮(图标经 IconKindConverter 安全解析;生产发布关掉上面的开关即隐藏)
  22. RegistryMonitorIcon.Kind = IconKindConverter.ToKind("BugOutline");
  23. RegistryMonitorButton.Visibility = EnableRegistryMonitorButton ? Visibility.Visible : Visibility.Collapsed;
  24. }
  25. private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  26. {
  27. // 关窗前先停采集并断开所有相机,再正常退出应用。
  28. // 不能用 Process.Kill():那是强制终止进程,OnExit 等一切清理事件都不会执行,
  29. // 相机句柄会残留,导致下次启动打不开设备。
  30. try { CameraManager.Instance.CloseAll(); } catch { }
  31. Application.Current.Shutdown();
  32. }
  33. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  34. {
  35. try
  36. {
  37. (DataContext as MainWindowViewModel)?.OnLoaded();
  38. }
  39. catch (System.Exception ex)
  40. {
  41. DialogHelper.ShowError("导航初始化失败", ex);
  42. }
  43. }
  44. /// <summary>
  45. /// 右上角齿轮按钮:code-behind 显式控制主题面板开关。
  46. /// 不走 IsOpen="{Binding IsChecked}" 绑定——非客户区(NonClientAreaContent)中
  47. /// 该绑定链不可靠;点击是否到达、面板是否打开全部由代码显式驱动。
  48. /// </summary>
  49. private void SettingsToggle_Click(object sender, RoutedEventArgs e)
  50. {
  51. var isOpen = SettingsToggle.IsChecked == true;
  52. TeamAAS.AppLogger.Info($"SettingsToggle_Click: IsChecked={isOpen}", nameof(MainWindow));
  53. if (ThemePopup == null) return;
  54. if (isOpen)
  55. {
  56. // 显式指定 Popup 内容的 DataContext(Popup 子树的 DataContext 继承在非客户区不可靠)
  57. ThemePopup.DataContext = DataContext;
  58. ThemePopup.IsOpen = true;
  59. }
  60. else
  61. {
  62. ThemePopup.IsOpen = false;
  63. }
  64. }
  65. /// <summary>面板因点击外部等被关闭时,同步齿轮按钮的勾选状态,防止下次点击翻转错乱</summary>
  66. private void ThemePopup_Closed(object sender, System.EventArgs e)
  67. {
  68. SettingsToggle.SetCurrentValue(System.Windows.Controls.Primitives.ToggleButton.IsCheckedProperty, false);
  69. }
  70. /// <summary>
  71. /// 【调试】打开 ResultRegistry 监控窗口(Main / Debug 并排显示,非模式单实例,已开则前置)。
  72. /// 生产环境通过 EnableRegistryMonitorButton=false 隐藏入口。
  73. /// </summary>
  74. private void RegistryMonitor_Click(object sender, RoutedEventArgs e)
  75. {
  76. try
  77. {
  78. if (_registryMonitorWindow != null)
  79. {
  80. _registryMonitorWindow.Activate();
  81. return;
  82. }
  83. _registryMonitorWindow = new Views.RegistryDebugWindow { Owner = this };
  84. _registryMonitorWindow.Closed += (s, args) => _registryMonitorWindow = null;
  85. _registryMonitorWindow.Show();
  86. }
  87. catch (System.Exception ex)
  88. {
  89. DialogHelper.ShowError("打开注册表监控失败", ex);
  90. }
  91. }
  92. }
  93. }