diff --git a/XplorePlane/App.xaml.cs b/XplorePlane/App.xaml.cs index 6f1bd73..53b18b7 100644 --- a/XplorePlane/App.xaml.cs +++ b/XplorePlane/App.xaml.cs @@ -13,6 +13,7 @@ using XP.Camera; using XP.Common.Configs; using XP.Common.Database.Implementations; using XP.Common.Database.Interfaces; +using XP.Common.GeneralForm.Views; using XP.Common.Dump.Configs; using XP.Common.Dump.Implementations; using XP.Common.Dump.Interfaces; @@ -149,7 +150,7 @@ namespace XplorePlane Log.Error(ex, "射线源资源释放失败"); } - // 释放相机服务资源 + // 释放导航相机服务资源 try { var bootstrapper = AppBootstrapper.Instance; @@ -157,12 +158,12 @@ namespace XplorePlane { var cameraService = bootstrapper.Container.Resolve(); cameraService?.Dispose(); - Log.Information("相机服务资源已释放"); + Log.Information("导航相机服务资源已释放"); } } catch (Exception ex) { - Log.Error(ex, "相机服务资源释放失败"); + Log.Error(ex, "导航相机服务资源释放失败"); } // 释放SQLite数据库资源 | Release SQLite database resources @@ -234,18 +235,55 @@ namespace XplorePlane protected override Window CreateShell() { // 提前初始化模块,确保硬件服务在 MainWindow XAML 解析前已注册 - // 默认 Prism 顺序是 CreateShell → InitializeModules, - // 但 MainWindow 中嵌入的硬件控件会在 XAML 解析时触发 ViewModelLocator, - // 此时模块尚未加载,导致依赖解析失败 if (!_modulesInitialized) { base.InitializeModules(); _modulesInitialized = true; } + // 在主窗口显示前完成导航相机连接,避免窗口弹出后再检索导致卡顿 + TryConnectCamera(); + return Container.Resolve(); } + /// + /// 在独立 MTA 线程中检索并连接导航相机,带 10 秒超时。 + /// 连接成功后 NavigationPropertyPanelViewModel 构造时拿到的就是已连接的导航相机。 + /// + private void TryConnectCamera() + { + var camera = Container.Resolve(); + CameraInfo? info = null; + Exception? error = null; + + var thread = new System.Threading.Thread(() => + { + try { info = camera.Open(); } + catch (Exception ex) { error = ex; } + }); + thread.IsBackground = true; + thread.SetApartmentState(System.Threading.ApartmentState.MTA); + thread.Start(); + + bool completed = thread.Join(TimeSpan.FromSeconds(10)); + + if (!completed) + { + Log.Warning("导航相机检索超时 (10s),跳过自动连接"); + HexMessageBox.Show("导航相机检索超时(10秒),请检查导航相机连接后重启软件。", MessageBoxButton.OK, MessageBoxImage.Error); + } + else if (error != null) + { + Log.Warning(error, "导航相机自动连接失败: {Message}", error.Message); + HexMessageBox.Show($"导航相机连接失败: {error.Message}", MessageBoxButton.OK, MessageBoxImage.Error); + } + else + { + Log.Information("导航相机已连接: {ModelName} (SN: {SerialNumber})", info!.ModelName, info.SerialNumber); + } + } + /// /// 模块已在 CreateShell 中提前初始化,此处跳过避免重复加载 /// @@ -327,7 +365,7 @@ namespace XplorePlane containerRegistry.RegisterForNavigation(); containerRegistry.RegisterForNavigation(); - // ── 相机服务(单例)── + // ── 导航相机服务(单例)── containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(() => new CameraFactory().CreateController("Basler")); diff --git a/XplorePlane/ViewModels/Main/NavigationPropertyPanelViewModel.cs b/XplorePlane/ViewModels/Main/NavigationPropertyPanelViewModel.cs index b48a240..62c1483 100644 --- a/XplorePlane/ViewModels/Main/NavigationPropertyPanelViewModel.cs +++ b/XplorePlane/ViewModels/Main/NavigationPropertyPanelViewModel.cs @@ -176,6 +176,24 @@ namespace XplorePlane.ViewModels ApplyPixelFormatCommand = new DelegateCommand(() => ApplyCameraParam(() => _camera.SetPixelFormat(SelectedPixelFormat)), () => IsCameraConnected); RefreshCameraParamsCommand = new DelegateCommand(RefreshCameraParams, () => IsCameraConnected); OpenCameraSettingsCommand = new DelegateCommand(OpenCameraSettings, () => IsCameraConnected); + + // 相机已在 App 启动阶段连接,直接检测状态并启动实时预览 + if (_camera.IsConnected) + { + _camera.ImageGrabbed += OnCameraImageGrabbed; + _camera.GrabError += OnCameraGrabError; + _camera.ConnectionLost += OnCameraConnectionLost; + + IsCameraConnected = true; + CameraStatusText = "已连接"; + RefreshCameraParams(); + StartGrab(); + IsLiveViewEnabled = true; + } + else + { + CameraStatusText = "未检测到相机"; + } } #region Camera Methods diff --git a/XplorePlane/Views/Main/NavigationPropertyPanelView.xaml b/XplorePlane/Views/Main/NavigationPropertyPanelView.xaml index 791f55d..df97103 100644 --- a/XplorePlane/Views/Main/NavigationPropertyPanelView.xaml +++ b/XplorePlane/Views/Main/NavigationPropertyPanelView.xaml @@ -12,7 +12,6 @@ - @@ -56,50 +55,5 @@ - - - - -