diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index d2c7c6f..3da939d 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -9,9 +9,11 @@ using System.Windows.Navigation; using WpfApp.src.components; using WpfApp.src.config; using WpfApp.src.view; +using WpfApp.src.controllers; using WpfApp.Utils; + namespace WpfApp; /// @@ -22,6 +24,7 @@ public partial class MainWindow : Window private readonly LogService log; private readonly DatabaseService db; private readonly EventService even; + private readonly MenuController menu; public MainWindow(LogService logService, DatabaseService databaseService, EventService eventService) { @@ -30,6 +33,8 @@ public partial class MainWindow : Window log = logService; db = databaseService; even = eventService; + // 初始化菜单控制器 + menu = new MenuController(this); // 记录窗口初始化日志 log.Info("MainWindow", "主窗口已通过依赖注入初始化"); @@ -81,40 +86,17 @@ public partial class MainWindow : Window - #region 菜单点击按钮 - - private void OnFilePageButtonClick(object sender, RoutedEventArgs e) - { - WindowHelper.ShowPageDialog(new FilePage(), "记录列表", 810, 600,isResizable: false, IconHelper.GetIconPath("main1-File.ico")); - } - - private void OnConfigPageButtonClick(object sender, RoutedEventArgs e) - { - WindowHelper.ShowPageDialog(new ConfigPage(), "配置", 972, 648, isResizable: false, IconHelper.GetIconPath("main2-Config.ico")); - } - - private void OnStandardPageButtonClick(object sender, RoutedEventArgs e) - { - WindowHelper.ShowPageDialog(new StandardPage(), "主标定", 972, 648, isResizable: true, IconHelper.GetIconPath("main3-Standard.ico")); - } - - private void OnGaugePageButtonClick(object sender, RoutedEventArgs e) - { - WindowHelper.ShowPageDialog(new GaugePage(), "Gauge R&&R", 1080, 720, isResizable:false, IconHelper.GetIconPath("main4-Gauge.ico")); - } - - private void OnCgCgkPageButtonClick(object sender, RoutedEventArgs e) - { - WindowHelper.ShowPageDialog(new CgCgkPage(), "CgCgk", 972, 648, isResizable: true, IconHelper.GetIconPath("main5-CgCgk.ico")); - } - - private void OnSysSetPageButtonClick(object sender, RoutedEventArgs e) - { - WindowHelper.ShowPageDialog(new SysSetPage(), "系统设置", 810, 600, isResizable:false, IconHelper.GetIconPath("main6-SysSet.ico")); - } - + #region 菜单点击按钮绑定 + private void OnFilePageButtonClick(object sender, RoutedEventArgs e) => menu.OpenFilePage(); + private void OnConfigPageButtonClick(object sender, RoutedEventArgs e) => menu.OpenConfigPage(); + private void OnStandardPageButtonClick(object sender, RoutedEventArgs e) => menu.OpenStandardPage(); + private void OnGaugePageButtonClick(object sender, RoutedEventArgs e) => menu.OpenGaugePage(); + private void OnCgCgkPageButtonClick(object sender, RoutedEventArgs e) => menu.OpenCgCgkPage(); + private void OnSysSetPageButtonClick(object sender, RoutedEventArgs e) => menu.OpenSysSetPage(); #endregion + + #region 关闭和测试按钮 /// /// 按钮点击触发关闭页面 diff --git a/Services/ChartManager.cs b/Services/ChartManager.cs new file mode 100644 index 0000000..1ec731e --- /dev/null +++ b/Services/ChartManager.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WpfApp.Services; + +public class ChartManager +{ +} diff --git a/Utils/IconHelper.cs b/Utils/IconHelper.cs index eed5cd0..d06f38d 100644 --- a/Utils/IconHelper.cs +++ b/Utils/IconHelper.cs @@ -18,7 +18,7 @@ namespace WpfApp.Utils iconFileName ??= defaultIconName; // 默认项目图标目录(相对于 EXE) - string projectIconDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\src\public\Icons"); + string projectIconDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\src\public"); // 先搜索项目目录 string path = SearchDirectory(projectIconDir, iconFileName); @@ -40,6 +40,34 @@ namespace WpfApp.Utils throw new FileNotFoundException($"找不到图标文件: {iconFileName}"); } + /// + /// 获取图标目录路径(智能推断) + /// + /// 可选外部搜索目录 + /// 图标目录的绝对路径 + public static string GetIconDirectoryPath(string[] externalDirs = null) + { + // 默认项目图标目录(相对于 EXE) + string projectIconDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\src\public"); + string fullPath = Path.GetFullPath(projectIconDir); + + if (Directory.Exists(fullPath)) + return fullPath; + + // 如果外部目录存在则返回第一个有效路径 + if (externalDirs != null) + { + foreach (var dir in externalDirs) + { + if (Directory.Exists(dir)) + return Path.GetFullPath(dir); + } + } + + // 否则抛异常 + throw new DirectoryNotFoundException($"找不到图标目录: {fullPath}"); + } + /// /// 在指定目录及子目录搜索文件 /// diff --git a/src/controllers/MenuController.cs b/src/controllers/MenuController.cs new file mode 100644 index 0000000..2a422d0 --- /dev/null +++ b/src/controllers/MenuController.cs @@ -0,0 +1,85 @@ +using System.Windows; +using WpfApp.src.view; +using WpfApp.Utils; + +namespace WpfApp.src.controllers +{ + /// + /// 菜单控制器 - 统一管理菜单打开逻辑 + /// + public class MenuController + { + private readonly Window mainWindow; + + public MenuController(Window window) + { + mainWindow = window; + } + + /// + /// 打开文件记录列表页面 + /// + public void OpenFilePage() + { + WindowHelper.ShowPageDialog(new FilePage(), + "记录列表", 810, 600, + isResizable: false, + IconHelper.GetIconPath("main1-File.ico")); + } + + /// + /// 打开配置页面 + /// + public void OpenConfigPage() + { + WindowHelper.ShowPageDialog(new ConfigPage(), + "配置", 972, 648, + isResizable: false, + IconHelper.GetIconPath("main2-Config.ico")); + } + + /// + /// 打开主标定页面 + /// + public void OpenStandardPage() + { + WindowHelper.ShowPageDialog(new StandardPage(), + "主标定", 972, 648, + isResizable: true, + IconHelper.GetIconPath("main3-Standard.ico")); + } + + /// + /// 打开 Gauge R&R 页面 + /// + public void OpenGaugePage() + { + WindowHelper.ShowPageDialog(new GaugePage(), + "Gauge R&&R", 1080, 720, + isResizable: false, + IconHelper.GetIconPath("main4-Gauge.ico")); + } + + /// + /// 打开 CgCgk 页面 + /// + public void OpenCgCgkPage() + { + WindowHelper.ShowPageDialog(new CgCgkPage(), + "CgCgk", 972, 648, + isResizable: true, + IconHelper.GetIconPath("main5-CgCgk.ico")); + } + + /// + /// 打开系统设置页面 + /// + public void OpenSysSetPage() + { + WindowHelper.ShowPageDialog(new SysSetPage(), + "系统设置", 810, 600, + isResizable: false, + IconHelper.GetIconPath("main6-SysSet.ico")); + } + } +} diff --git a/src/public/Excel/Report_2025_10_13.xlsx b/src/public/Excel/Report_2025_10_13.xlsx index 8130b34..b8f9606 100644 Binary files a/src/public/Excel/Report_2025_10_13.xlsx and b/src/public/Excel/Report_2025_10_13.xlsx differ diff --git a/src/view/CgCgkPage.xaml.cs b/src/view/CgCgkPage.xaml.cs index 78e46ff..3606bbe 100644 --- a/src/view/CgCgkPage.xaml.cs +++ b/src/view/CgCgkPage.xaml.cs @@ -18,29 +18,17 @@ public partial class CgCgkPage : Page private void OnCgCgkPageButtonClick(object sender, RoutedEventArgs e) { - string templatePath = @"C:\Users\yongye\Desktop\code\code\SolartronMetrologyWpfApp\WpfApp\src\public\Excel\demo.xlsx"; - string savePath = @"C:\Users\yongye\Desktop\code\code\SolartronMetrologyWpfApp\WpfApp\src\public\Excel\Report_2025_10_13.xlsx"; - - // 1️⃣ 加载模板 - ExcelHelper.LoadTemplate(templatePath); - - // 2️⃣ 选择工作表 + ExcelHelper.LoadTemplate(IconHelper.GetIconPath("demo.xlsx")); ExcelHelper.SelectSheet("表2"); - - // 3️⃣ 插入数据 ExcelHelper.InsertRowData(5, 0, new List { "传感器1", 25.6, 101.3, DateTime.Now }); - - // 4️⃣ 修改单元格 ExcelHelper.WriteCell("B2", "测试人员"); ExcelHelper.WriteCell("C3", DateTime.Now.ToString("yyyy-MM-dd")); + ExcelHelper.SaveAs(IconHelper.GetIconDirectoryPath() + "\\Excel\\Report_2025_10_13.xlsx"); - // 5️⃣ 保存为新文件 - ExcelHelper.SaveAs(savePath); + // 在页面控件上显示保存后的 Excel + grid.Load(IconHelper.GetIconPath("Report_2025_10_13.xlsx")); - // 6️⃣ 在页面控件上显示保存后的 Excel - grid.Load(savePath); // 这里直接加载 Report_2025_10_13.xlsx - - // 7️⃣ 不要 Dispose,否则控件被销毁 + // 不要 Dispose,否则控件被销毁 // ExcelHelper.Dispose(); } diff --git a/src/view/FilePage.xaml.cs b/src/view/FilePage.xaml.cs index 0fcbbdc..03955e6 100644 --- a/src/view/FilePage.xaml.cs +++ b/src/view/FilePage.xaml.cs @@ -1,153 +1,140 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; using System.Windows.Navigation; -using System.Windows.Shapes; +namespace WpfApp.src.view; -namespace WpfApp.src.view +/// +/// FilePage.xaml 的交互逻辑 +/// +public partial class FilePage : Page { - /// - /// FilePage.xaml 的交互逻辑 - /// - public partial class FilePage : Page + public FilePage() { - public FilePage() + InitializeComponent(); + LoadSampleData(); + } + + private void LoadSampleData() + { + // 清空现有项目 + RecordListBox.Items.Clear(); + + // 添加示例数据 + var records = new List { - InitializeComponent(); - LoadSampleData(); + "1. GGG[2021-11-17 7:39:50]", + "2. MTU SG2000[2021-12-2 7:41:55]", + "3. JX493ZQ4[2022-1-8 11:22:42]", + "4. JX493ZQ5A[2022-2-15 1:26:38]", + "5. FOTON D01[2022-9-1 9:06:43]", + "6. PUMA2.0[2022-9-15 8:47:59]", + "7. PUMA2.2[2022-9-21 7:50:39]", + "8. SAIC D20[2022-10-10 8:27:46]", + "9. WEICHAI WP2H[2023-3-30 8:46:02]", + "10. JMC PUMA UPG 2.0[2023-7-10 17:07:07]", + "11. BFCEC ISF2.8L F2020 NS6[2023-7-10 17:21:50]", + "12. 12[2023-8-21 14:44:10]", + "13. GME T4[2023-10-2 8:55:38]", + "14. GW EN01[2023-10-4 8:12:50]", + "15. GAC P26 2.0PMI[2023-11-9 7:44:42]", + "16. NU PE2.0 TGDI[2024-5-2 11:07:36]", + "17. M254 E20[2024-7-2 10:08:06]", + "18. NU PE2.0MPI[2024-10-29 10:45:14]" + }; + + foreach (var record in records) + { + RecordListBox.Items.Add(record); + } + } + + private void SearchButton_Click(object sender, RoutedEventArgs e) + { + string searchText = SearchTextBox.Text.Trim(); + if (string.IsNullOrEmpty(searchText)) + { + LoadSampleData(); // 重新加载所有数据 + return; } - private void LoadSampleData() + // 过滤数据 + RecordListBox.Items.Clear(); + var allRecords = new List { - // 清空现有项目 - RecordListBox.Items.Clear(); + "1. GGG[2021-11-17 7:39:50]", + "2. MTU SG2000[2021-12-2 7:41:55]", + "3. JX493ZQ4[2022-1-8 11:22:42]", + "4. JX493ZQ5A[2022-2-15 1:26:38]", + "5. FOTON D01[2022-9-1 9:06:43]", + "6. PUMA2.0[2022-9-15 8:47:59]", + "7. PUMA2.2[2022-9-21 7:50:39]", + "8. SAIC D20[2022-10-10 8:27:46]", + "9. WEICHAI WP2H[2023-3-30 8:46:02]", + "10. JMC PUMA UPG 2.0[2023-7-10 17:07:07]", + "11. BFCEC ISF2.8L F2020 NS6[2023-7-10 17:21:50]", + "12. 12[2023-8-21 14:44:10]", + "13. GME T4[2023-10-2 8:55:38]", + "14. GW EN01[2023-10-4 8:12:50]", + "15. GAC P26 2.0PMI[2023-11-9 7:44:42]", + "16. NU PE2.0 TGDI[2024-5-2 11:07:36]", + "17. M254 E20[2024-7-2 10:08:06]", + "18. NU PE2.0MPI[2024-10-29 10:45:14]" + }; + + var filteredRecords = allRecords.Where(r => r.ToLower().Contains(searchText.ToLower())).ToList(); + foreach (var record in filteredRecords) + { + RecordListBox.Items.Add(record); + } + } + + private void DeleteButton_Click(object sender, RoutedEventArgs e) + { + if (RecordListBox.SelectedItem != null) + { + var result = MessageBox.Show("确定要删除选中的记录吗?", "确认删除", + MessageBoxButton.YesNo, MessageBoxImage.Question); - // 添加示例数据 - var records = new List + if (result == MessageBoxResult.Yes) { - "1. GGG[2021-11-17 7:39:50]", - "2. MTU SG2000[2021-12-2 7:41:55]", - "3. JX493ZQ4[2022-1-8 11:22:42]", - "4. JX493ZQ5A[2022-2-15 1:26:38]", - "5. FOTON D01[2022-9-1 9:06:43]", - "6. PUMA2.0[2022-9-15 8:47:59]", - "7. PUMA2.2[2022-9-21 7:50:39]", - "8. SAIC D20[2022-10-10 8:27:46]", - "9. WEICHAI WP2H[2023-3-30 8:46:02]", - "10. JMC PUMA UPG 2.0[2023-7-10 17:07:07]", - "11. BFCEC ISF2.8L F2020 NS6[2023-7-10 17:21:50]", - "12. 12[2023-8-21 14:44:10]", - "13. GME T4[2023-10-2 8:55:38]", - "14. GW EN01[2023-10-4 8:12:50]", - "15. GAC P26 2.0PMI[2023-11-9 7:44:42]", - "16. NU PE2.0 TGDI[2024-5-2 11:07:36]", - "17. M254 E20[2024-7-2 10:08:06]", - "18. NU PE2.0MPI[2024-10-29 10:45:14]" - }; - - foreach (var record in records) - { - RecordListBox.Items.Add(record); + RecordListBox.Items.Remove(RecordListBox.SelectedItem); } } - - private void SearchButton_Click(object sender, RoutedEventArgs e) + else { - string searchText = SearchTextBox.Text.Trim(); - if (string.IsNullOrEmpty(searchText)) - { - LoadSampleData(); // 重新加载所有数据 - return; - } - - // 过滤数据 - RecordListBox.Items.Clear(); - var allRecords = new List - { - "1. GGG[2021-11-17 7:39:50]", - "2. MTU SG2000[2021-12-2 7:41:55]", - "3. JX493ZQ4[2022-1-8 11:22:42]", - "4. JX493ZQ5A[2022-2-15 1:26:38]", - "5. FOTON D01[2022-9-1 9:06:43]", - "6. PUMA2.0[2022-9-15 8:47:59]", - "7. PUMA2.2[2022-9-21 7:50:39]", - "8. SAIC D20[2022-10-10 8:27:46]", - "9. WEICHAI WP2H[2023-3-30 8:46:02]", - "10. JMC PUMA UPG 2.0[2023-7-10 17:07:07]", - "11. BFCEC ISF2.8L F2020 NS6[2023-7-10 17:21:50]", - "12. 12[2023-8-21 14:44:10]", - "13. GME T4[2023-10-2 8:55:38]", - "14. GW EN01[2023-10-4 8:12:50]", - "15. GAC P26 2.0PMI[2023-11-9 7:44:42]", - "16. NU PE2.0 TGDI[2024-5-2 11:07:36]", - "17. M254 E20[2024-7-2 10:08:06]", - "18. NU PE2.0MPI[2024-10-29 10:45:14]" - }; - - var filteredRecords = allRecords.Where(r => r.ToLower().Contains(searchText.ToLower())).ToList(); - foreach (var record in filteredRecords) - { - RecordListBox.Items.Add(record); - } + MessageBox.Show("请先选择要删除的记录。", "提示", + MessageBoxButton.OK, MessageBoxImage.Information); } + } - private void DeleteButton_Click(object sender, RoutedEventArgs e) + private void SelectButton_Click(object sender, RoutedEventArgs e) + { + if (RecordListBox.SelectedItem != null) { - if (RecordListBox.SelectedItem != null) - { - var result = MessageBox.Show("确定要删除选中的记录吗?", "确认删除", - MessageBoxButton.YesNo, MessageBoxImage.Question); - - if (result == MessageBoxResult.Yes) - { - RecordListBox.Items.Remove(RecordListBox.SelectedItem); - } - } - else - { - MessageBox.Show("请先选择要删除的记录。", "提示", - MessageBoxButton.OK, MessageBoxImage.Information); - } + string selectedRecord = RecordListBox.SelectedItem.ToString(); + MessageBox.Show($"已选择记录:{selectedRecord}", "选择确认", + MessageBoxButton.OK, MessageBoxImage.Information); + + // 这里可以添加实际的选择逻辑,比如返回到主界面或执行其他操作 } - - private void SelectButton_Click(object sender, RoutedEventArgs e) + else { - if (RecordListBox.SelectedItem != null) - { - string selectedRecord = RecordListBox.SelectedItem.ToString(); - MessageBox.Show($"已选择记录:{selectedRecord}", "选择确认", - MessageBoxButton.OK, MessageBoxImage.Information); - - // 这里可以添加实际的选择逻辑,比如返回到主界面或执行其他操作 - } - else - { - MessageBox.Show("请先选择一条记录。", "提示", - MessageBoxButton.OK, MessageBoxImage.Information); - } + MessageBox.Show("请先选择一条记录。", "提示", + MessageBoxButton.OK, MessageBoxImage.Information); } + } - private void CloseButton_Click(object sender, RoutedEventArgs e) + private void CloseButton_Click(object sender, RoutedEventArgs e) + { + // 关闭当前页面或返回上一页 + if (NavigationService.CanGoBack) { - // 关闭当前页面或返回上一页 - if (NavigationService.CanGoBack) - { - NavigationService.GoBack(); - } - else - { - // 如果无法返回,可以关闭窗口或执行其他操作 - Window.GetWindow(this)?.Close(); - } + NavigationService.GoBack(); + } + else + { + // 如果无法返回,可以关闭窗口或执行其他操作 + Window.GetWindow(this)?.Close(); } } }