diff --git a/App.xaml.cs b/App.xaml.cs index a270fdf..d7b2f21 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -17,20 +17,44 @@ namespace DrillTools base.OnStartup(e); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); - // 运行孔位数据功能测试 - //MainWindowViewModel.TestHoleLocationsFunctionality(); - - // 检查是否有命令行参数(拖拽文件到exe上) string? filePath = null; if (e.Args.Length > 0) { - // 获取第一个参数作为文件路径 filePath = e.Args[0]; - // 验证文件是否存在且为有效的钻带文件 if (!IsValidDrillTapeFile(filePath)) - filePath = null; // 重置为null,按正常启动流程 + filePath = null; } + if (filePath == null) + { + ShowMainWindow(null); + return; + } + + // 防止选择窗口关闭后触发 OnLastWindowClose 导致应用退出 + ShutdownMode = ShutdownMode.OnExplicitShutdown; + + var selectionWindow = new StartupSelectionWindow(filePath); + selectionWindow.ShowDialog(); + + switch (selectionWindow.SelectedAction) + { + case StartupAction.AdjustToolOrder: + ShutdownMode = ShutdownMode.OnLastWindowClose; + ShowMainWindow(filePath); + break; + case StartupAction.ExportHoleCount: + PerformHeadlessExport(filePath); + Shutdown(); + break; + default: + Shutdown(); + break; + } + } + + private static void ShowMainWindow(string? filePath) + { try { MainWindow mainWindow = new MainWindow(filePath); @@ -38,13 +62,41 @@ namespace DrillTools } catch (Exception ex) { - // 显示错误消息 System.Windows.MessageBox.Show($"创建MainWindow时发生异常:\n{ex.GetType().Name} - {ex.Message}\n\n{ex.StackTrace}", - "启动失败", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); + "启动失败", MessageBoxButton.OK, MessageBoxImage.Error); throw; } } + private static void PerformHeadlessExport(string filePath) + { + try + { + var viewModel = new MainWindowViewModel(); + + string content = CommandTypeFileReader.ReadAllText(filePath); + viewModel.OriginalFilePath = filePath; + viewModel.LoadToolsFromDrillTape(content); + + if (viewModel.Tools.Count == 0) + { + System.Windows.MessageBox.Show("钻带文件中未找到有效的刀具数据,无法导出孔数报表。", + "提示", MessageBoxButton.OK, MessageBoxImage.Warning); + return; + } + + viewModel.ExportDrillUsageReport(); + } + catch (OperationCanceledException) + { + } + catch (Exception ex) + { + System.Windows.MessageBox.Show($"导出孔数报表失败:\n{ex.Message}", + "错误", MessageBoxButton.OK, MessageBoxImage.Error); + } + } + /// /// 验证是否为有效的钻带文件 /// diff --git a/StartupSelectionWindow.xaml b/StartupSelectionWindow.xaml new file mode 100644 index 0000000..a4192df --- /dev/null +++ b/StartupSelectionWindow.xaml @@ -0,0 +1,24 @@ + + + + + + + + + + + + +