新增启动菜单

This commit is contained in:
2026-05-17 21:56:57 +08:00
parent e006f83a19
commit a27cef82b2
3 changed files with 120 additions and 9 deletions

View File

@@ -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);
}
}
/// <summary>
/// 验证是否为有效的钻带文件
/// </summary>

View File

@@ -0,0 +1,24 @@
<Window x:Class="DrillTools.StartupSelectionWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="选择功能"
Width="300"
SizeToContent="Height"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen">
<Grid Margin="15">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding FileName}" FontWeight="Bold" TextWrapping="Wrap" Margin="0,0,0,10"/>
<TextBlock Grid.Row="1" Text="请选择要执行的功能:" Margin="0,0,0,15"/>
<StackPanel Grid.Row="2" HorizontalAlignment="Center" Orientation="Horizontal">
<Button Width="120" Height="30" Content="调整刀序" Margin="0,0,15,0" Click="AdjustToolOrder_Click"/>
<Button Width="120" Height="30" Content="导出孔数" Click="ExportHoleCount_Click"/>
</StackPanel>
</Grid>
</Window>

View File

@@ -0,0 +1,35 @@
using System.IO;
using System.Windows;
namespace DrillTools
{
public enum StartupAction
{
None,
AdjustToolOrder,
ExportHoleCount
}
public partial class StartupSelectionWindow : Window
{
public StartupAction SelectedAction { get; private set; } = StartupAction.None;
public StartupSelectionWindow(string filePath)
{
InitializeComponent();
DataContext = new { FileName = Path.GetFileName(filePath) };
}
private void AdjustToolOrder_Click(object sender, RoutedEventArgs e)
{
SelectedAction = StartupAction.AdjustToolOrder;
Close();
}
private void ExportHoleCount_Click(object sender, RoutedEventArgs e)
{
SelectedAction = StartupAction.ExportHoleCount;
Close();
}
}
}