支持拖拽钻带文件到exe自动打开并加载
实现了命令行参数解析,支持将钻带文件(.txt, .drl, .dr2, .dpin)拖拽到exe上直接打开。重构了 App 启动流程,MainWindow 构造函数支持初始文件路径,窗口加载后自动异步读取并解析文件内容。增强了异常处理,提升了启动健壮性和用户体验。
This commit is contained in:
@@ -13,15 +13,117 @@ namespace DrillTools
|
||||
{
|
||||
private MainWindowViewModel ViewModel => (MainWindowViewModel)DataContext;
|
||||
|
||||
public MainWindow()
|
||||
public MainWindow(string? initialFilePath = null)
|
||||
{
|
||||
InitializeComponent();
|
||||
var viewModel = new MainWindowViewModel();
|
||||
DataContext = viewModel;
|
||||
InitializeDragDrop();
|
||||
//System.Diagnostics.Debug.WriteLine("=== MainWindow构造函数开始执行 ===");
|
||||
//System.Diagnostics.Debug.WriteLine($"参数 initialFilePath: {initialFilePath ?? "null"}");
|
||||
|
||||
// 设置默认置顶状态
|
||||
this.Topmost = viewModel.IsTopmost;
|
||||
try
|
||||
{
|
||||
//System.Diagnostics.Debug.WriteLine("调用 InitializeComponent()...");
|
||||
InitializeComponent();
|
||||
//System.Diagnostics.Debug.WriteLine("InitializeComponent() 执行完成");
|
||||
|
||||
//System.Diagnostics.Debug.WriteLine("创建 MainWindowViewModel...");
|
||||
var viewModel = new MainWindowViewModel();
|
||||
//System.Diagnostics.Debug.WriteLine("MainWindowViewModel 创建完成");
|
||||
|
||||
//System.Diagnostics.Debug.WriteLine("设置 DataContext...");
|
||||
DataContext = viewModel;
|
||||
//System.Diagnostics.Debug.WriteLine("DataContext 设置完成");
|
||||
|
||||
//System.Diagnostics.Debug.WriteLine("初始化拖放功能...");
|
||||
InitializeDragDrop();
|
||||
//System.Diagnostics.Debug.WriteLine("拖放功能初始化完成");
|
||||
|
||||
// 设置默认置顶状态
|
||||
this.Topmost = viewModel.IsTopmost;
|
||||
//System.Diagnostics.Debug.WriteLine("置顶状态设置完成");
|
||||
|
||||
// 如果有初始文件路径,在窗口加载完成后自动加载
|
||||
if (!string.IsNullOrEmpty(initialFilePath))
|
||||
{
|
||||
//System.Diagnostics.Debug.WriteLine("注册文件加载事件...");
|
||||
this.Loaded += (sender, e) => LoadInitialFile(initialFilePath, viewModel);
|
||||
//System.Diagnostics.Debug.WriteLine("文件加载事件注册完成");
|
||||
}
|
||||
|
||||
//System.Diagnostics.Debug.WriteLine("=== MainWindow构造函数执行完成 ===");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//System.Diagnostics.Debug.WriteLine($"MainWindow构造函数中发生异常: {ex.GetType().Name} - {ex.Message}");
|
||||
//System.Diagnostics.Debug.WriteLine($"异常堆栈: {ex.StackTrace}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载初始文件
|
||||
/// </summary>
|
||||
/// <param name="filePath">文件路径</param>
|
||||
/// <param name="viewModel">视图模型</param>
|
||||
private async void LoadInitialFile(string filePath, MainWindowViewModel viewModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 异步加载文件以避免阻塞UI
|
||||
await System.Threading.Tasks.Task.Run(() =>
|
||||
{
|
||||
// 使用与现有代码相同的方式读取文件
|
||||
var process = new System.Diagnostics.Process
|
||||
{
|
||||
StartInfo = new System.Diagnostics.ProcessStartInfo
|
||||
{
|
||||
FileName = "cmd.exe",
|
||||
Arguments = $"/c type \"{filePath}\"",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
string drillTapeContent = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
|
||||
// 在UI线程中更新界面
|
||||
this.Dispatcher.Invoke(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// 保存原始文件路径
|
||||
viewModel.OriginalFilePath = filePath;
|
||||
|
||||
// 加载钻带内容
|
||||
viewModel.LoadToolsFromDrillTape(drillTapeContent);
|
||||
|
||||
// 显示成功提示
|
||||
//System.Windows.MessageBox.Show(
|
||||
// $"已成功加载钻带文件:\n{System.IO.Path.GetFileName(filePath)}\n\n刀具数量:{viewModel.Tools.Count}把",
|
||||
// "文件加载完成",
|
||||
// System.Windows.MessageBoxButton.OK,
|
||||
// System.Windows.MessageBoxImage.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Windows.MessageBox.Show(
|
||||
$"加载钻带文件失败:\n{ex.Message}",
|
||||
"错误",
|
||||
System.Windows.MessageBoxButton.OK,
|
||||
System.Windows.MessageBoxImage.Error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Windows.MessageBox.Show(
|
||||
$"读取文件时发生错误:\n{ex.Message}",
|
||||
"错误",
|
||||
System.Windows.MessageBoxButton.OK,
|
||||
System.Windows.MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -98,7 +200,7 @@ namespace DrillTools
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// 用户取消操作,不显示错误消息
|
||||
System.Diagnostics.Debug.WriteLine("用户取消了刀序重排操作");
|
||||
//System.Diagnostics.Debug.WriteLine("用户取消了刀序重排操作");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -120,7 +222,7 @@ namespace DrillTools
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// 用户取消操作,不显示错误消息
|
||||
System.Diagnostics.Debug.WriteLine("用户取消了保存操作");
|
||||
//System.Diagnostics.Debug.WriteLine("用户取消了保存操作");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -141,7 +243,7 @@ namespace DrillTools
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// 用户取消操作,不显示错误消息
|
||||
System.Diagnostics.Debug.WriteLine("用户取消了生成排序种子操作");
|
||||
//System.Diagnostics.Debug.WriteLine("用户取消了生成排序种子操作");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user