支持拖拽钻带文件到exe自动打开并加载
实现了命令行参数解析,支持将钻带文件(.txt, .drl, .dr2, .dpin)拖拽到exe上直接打开。重构了 App 启动流程,MainWindow 构造函数支持初始文件路径,窗口加载后自动异步读取并解析文件内容。增强了异常处理,提升了启动健壮性和用户体验。
This commit is contained in:
3
App.xaml
3
App.xaml
@@ -2,8 +2,7 @@
|
|||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:local="clr-namespace:DrillTools"
|
xmlns:local="clr-namespace:DrillTools"
|
||||||
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
xmlns:sys="clr-namespace:System;assembly=mscorlib">
|
||||||
StartupUri="MainWindow.xaml">
|
|
||||||
<Application.Resources>
|
<Application.Resources>
|
||||||
<!-- 布尔值到可见性转换器 -->
|
<!-- 布尔值到可见性转换器 -->
|
||||||
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
|
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
|
||||||
|
|||||||
48
App.xaml.cs
48
App.xaml.cs
@@ -1,5 +1,7 @@
|
|||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
|
||||||
namespace DrillTools
|
namespace DrillTools
|
||||||
@@ -16,9 +18,49 @@ namespace DrillTools
|
|||||||
// 运行孔位数据功能测试
|
// 运行孔位数据功能测试
|
||||||
//MainWindowViewModel.TestHoleLocationsFunctionality();
|
//MainWindowViewModel.TestHoleLocationsFunctionality();
|
||||||
|
|
||||||
// 创建并显示主窗口
|
// 检查是否有命令行参数(拖拽文件到exe上)
|
||||||
//MainWindow mainWindow = new MainWindow();
|
string? filePath = null;
|
||||||
//mainWindow.Show();
|
if (e.Args.Length > 0)
|
||||||
|
{
|
||||||
|
// 获取第一个参数作为文件路径
|
||||||
|
filePath = e.Args[0];
|
||||||
|
// 验证文件是否存在且为有效的钻带文件
|
||||||
|
if (!IsValidDrillTapeFile(filePath))
|
||||||
|
filePath = null; // 重置为null,按正常启动流程
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
MainWindow mainWindow = new MainWindow(filePath);
|
||||||
|
mainWindow.Show();
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 验证是否为有效的钻带文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filePath">文件路径</param>
|
||||||
|
/// <returns>是否为有效钻带文件</returns>
|
||||||
|
private static bool IsValidDrillTapeFile(string filePath)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(filePath))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!File.Exists(filePath))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// 检查文件扩展名是否为支持的钻带文件格式
|
||||||
|
string extension = Path.GetExtension(filePath).ToLowerInvariant();
|
||||||
|
string[] supportedExtensions = { ".txt", ".drl", ".dr2", ".dpin" };
|
||||||
|
|
||||||
|
return supportedExtensions.Contains(extension);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,15 +13,117 @@ namespace DrillTools
|
|||||||
{
|
{
|
||||||
private MainWindowViewModel ViewModel => (MainWindowViewModel)DataContext;
|
private MainWindowViewModel ViewModel => (MainWindowViewModel)DataContext;
|
||||||
|
|
||||||
public MainWindow()
|
public MainWindow(string? initialFilePath = null)
|
||||||
{
|
{
|
||||||
|
//System.Diagnostics.Debug.WriteLine("=== MainWindow构造函数开始执行 ===");
|
||||||
|
//System.Diagnostics.Debug.WriteLine($"参数 initialFilePath: {initialFilePath ?? "null"}");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//System.Diagnostics.Debug.WriteLine("调用 InitializeComponent()...");
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
//System.Diagnostics.Debug.WriteLine("InitializeComponent() 执行完成");
|
||||||
|
|
||||||
|
//System.Diagnostics.Debug.WriteLine("创建 MainWindowViewModel...");
|
||||||
var viewModel = new MainWindowViewModel();
|
var viewModel = new MainWindowViewModel();
|
||||||
|
//System.Diagnostics.Debug.WriteLine("MainWindowViewModel 创建完成");
|
||||||
|
|
||||||
|
//System.Diagnostics.Debug.WriteLine("设置 DataContext...");
|
||||||
DataContext = viewModel;
|
DataContext = viewModel;
|
||||||
|
//System.Diagnostics.Debug.WriteLine("DataContext 设置完成");
|
||||||
|
|
||||||
|
//System.Diagnostics.Debug.WriteLine("初始化拖放功能...");
|
||||||
InitializeDragDrop();
|
InitializeDragDrop();
|
||||||
|
//System.Diagnostics.Debug.WriteLine("拖放功能初始化完成");
|
||||||
|
|
||||||
// 设置默认置顶状态
|
// 设置默认置顶状态
|
||||||
this.Topmost = viewModel.IsTopmost;
|
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>
|
/// <summary>
|
||||||
@@ -98,7 +200,7 @@ namespace DrillTools
|
|||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
// 用户取消操作,不显示错误消息
|
// 用户取消操作,不显示错误消息
|
||||||
System.Diagnostics.Debug.WriteLine("用户取消了刀序重排操作");
|
//System.Diagnostics.Debug.WriteLine("用户取消了刀序重排操作");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -120,7 +222,7 @@ namespace DrillTools
|
|||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
// 用户取消操作,不显示错误消息
|
// 用户取消操作,不显示错误消息
|
||||||
System.Diagnostics.Debug.WriteLine("用户取消了保存操作");
|
//System.Diagnostics.Debug.WriteLine("用户取消了保存操作");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -141,7 +243,7 @@ namespace DrillTools
|
|||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
// 用户取消操作,不显示错误消息
|
// 用户取消操作,不显示错误消息
|
||||||
System.Diagnostics.Debug.WriteLine("用户取消了生成排序种子操作");
|
//System.Diagnostics.Debug.WriteLine("用户取消了生成排序种子操作");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user