using System; using System.Diagnostics; using System.Text; using System.Windows; using System.Windows.Controls; namespace DrillTools { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private MainWindowViewModel ViewModel => (MainWindowViewModel)DataContext; public MainWindow(string? initialFilePath = null) { //System.Diagnostics.Debug.WriteLine("=== MainWindow构造函数开始执行 ==="); //System.Diagnostics.Debug.WriteLine($"参数 initialFilePath: {initialFilePath ?? "null"}"); 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; } } /// /// 加载初始文件 /// /// 文件路径 /// 视图模型 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); } } /// /// 初始化拖放功能 /// private void InitializeDragDrop() { DragDropHelper.EnableDragDrop(ToolsListView, InsertionIndicator); } /// /// 加载示例数据按钮点击事件 /// private void LoadSampleDataButton_Click(object sender, RoutedEventArgs e) { ViewModel.LoadSampleData(); } /// /// 加载钻带文件按钮点击事件 /// private void LoadDrillTapeButton_Click(object sender, RoutedEventArgs e) { var openFileDialog = new Microsoft.Win32.OpenFileDialog { Filter = "钻带文件 (*.txt;*.drl;*.dr2;*.dpin)|*.txt;*.drl;*.dr2;*.dpin|所有文件 (*.*)|*.*", Title = "选择钻带文件" }; if (openFileDialog.ShowDialog() == true) { try { // 保存原始文件路径 ViewModel.OriginalFilePath = openFileDialog.FileName; // 使用 cmd 命令读取加密钻带文件 var process = new Process { StartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c type \"{openFileDialog.FileName}\"", RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true } }; process.Start(); string drillTapeContent = process.StandardOutput.ReadToEnd(); process.WaitForExit(); ViewModel.LoadToolsFromDrillTape(drillTapeContent); } catch (Exception ex) { System.Windows.MessageBox.Show($"加载钻带文件失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } } /// /// 重排刀序按钮点击事件 /// private void ReorderToolsButton_Click(object sender, RoutedEventArgs e) { try { string reorderedDrillTape = ViewModel.ReorderAndRenumberTools(); ViewModel.DrillTapeContent = reorderedDrillTape; System.Windows.MessageBox.Show("刀序重排完成", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } catch (OperationCanceledException) { // 用户取消操作,不显示错误消息 //System.Diagnostics.Debug.WriteLine("用户取消了刀序重排操作"); } catch (Exception ex) { System.Windows.MessageBox.Show($"重排刀序失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// 应用顺序到钻带按钮点击事件 /// private void ApplyOrderButton_Click(object sender, RoutedEventArgs e) { try { string reorderedDrillTape = ViewModel.ReorderAndRenumberTools(true); ViewModel.DrillTapeContent = reorderedDrillTape; System.Windows.MessageBox.Show("刀具顺序已应用到钻带", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } catch (OperationCanceledException) { // 用户取消操作,不显示错误消息 //System.Diagnostics.Debug.WriteLine("用户取消了保存操作"); } catch (Exception ex) { System.Windows.MessageBox.Show($"应用刀具顺序失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// 生成排序种子按钮点击事件 /// private void GenerateSortSeedButton_Click(object sender, RoutedEventArgs e) { try { ViewModel.GenerateGeneralSortSeedFile(ViewModel.Tools); System.Windows.MessageBox.Show("通用排序种子文件已生成", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } catch (OperationCanceledException) { // 用户取消操作,不显示错误消息 //System.Diagnostics.Debug.WriteLine("用户取消了生成排序种子操作"); } catch (Exception ex) { System.Windows.MessageBox.Show($"生成排序种子失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// 使用指定钻带的刀序按钮点击事件 /// private void UseReferenceDrillTapeButton_Click(object sender, RoutedEventArgs e) { ViewModel.ReorderToolsByReferenceDrillTape(); } /// /// 使用指定种子的刀序按钮点击事件 /// private void UseSortSeedButton_Click(object sender, RoutedEventArgs e) { ViewModel.ReorderToolsBySortSeedFile(); } /// /// 测试参考钻带功能按钮点击事件 /// private void TestReferenceDrillTapeButton_Click(object sender, RoutedEventArgs e) { ViewModel.TestReorderByReferenceDrillTape(); } /// /// 上移按钮点击事件 /// private void MoveUpButton_Click(object sender, RoutedEventArgs e) { ViewModel.MoveSelectedToolUp(); } /// /// 下移按钮点击事件 /// private void MoveDownButton_Click(object sender, RoutedEventArgs e) { ViewModel.MoveSelectedToolDown(); } /// /// 置顶按钮点击事件 /// private void ToggleTopmostButton_Click(object sender, RoutedEventArgs e) { ViewModel.ToggleTopmost(); this.Topmost = ViewModel.IsTopmost; } /// /// 刀具列表双击事件 /// private void ToolsListView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) { if (sender is System.Windows.Controls.ListView listView && listView.SelectedItem is ToolItem selectedTool) { ViewModel.ShowToolDetail(selectedTool); } } /// /// 拖放文件进入窗口事件 /// protected override void OnDragEnter(System.Windows.DragEventArgs e) { if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop)) { e.Effects = System.Windows.DragDropEffects.Copy; } else { e.Effects = System.Windows.DragDropEffects.None; } base.OnDragEnter(e); } /// /// 拖放文件到窗口事件 /// protected override void OnDrop(System.Windows.DragEventArgs e) { if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop)) { string[] files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop); if (files.Length > 0) { try { // 保存原始文件路径 ViewModel.OriginalFilePath = files[0]; var process = new Process { StartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c type \"{files[0]}\"", RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true } }; process.Start(); string drillTapeContent = process.StandardOutput.ReadToEnd(); process.WaitForExit(); ViewModel.LoadToolsFromDrillTape(drillTapeContent); } catch (Exception ex) { System.Windows.MessageBox.Show($"加载钻带文件失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } } base.OnDrop(e); } } }