添加菜单控制器

This commit is contained in:
YONGYE 2025-10-13 15:00:50 +08:00
parent 3a11035329
commit 6e14f38a41
7 changed files with 258 additions and 177 deletions

View File

@ -9,9 +9,11 @@ using System.Windows.Navigation;
using WpfApp.src.components; using WpfApp.src.components;
using WpfApp.src.config; using WpfApp.src.config;
using WpfApp.src.view; using WpfApp.src.view;
using WpfApp.src.controllers;
using WpfApp.Utils; using WpfApp.Utils;
namespace WpfApp; namespace WpfApp;
/// <summary> /// <summary>
@ -22,6 +24,7 @@ public partial class MainWindow : Window
private readonly LogService log; private readonly LogService log;
private readonly DatabaseService db; private readonly DatabaseService db;
private readonly EventService<GeneralEventArgs> even; private readonly EventService<GeneralEventArgs> even;
private readonly MenuController menu;
public MainWindow(LogService logService, DatabaseService databaseService, EventService<GeneralEventArgs> eventService) public MainWindow(LogService logService, DatabaseService databaseService, EventService<GeneralEventArgs> eventService)
{ {
@ -30,6 +33,8 @@ public partial class MainWindow : Window
log = logService; log = logService;
db = databaseService; db = databaseService;
even = eventService; even = eventService;
// 初始化菜单控制器
menu = new MenuController(this);
// 记录窗口初始化日志 // 记录窗口初始化日志
log.Info("MainWindow", "主窗口已通过依赖注入初始化"); log.Info("MainWindow", "主窗口已通过依赖注入初始化");
@ -81,40 +86,17 @@ public partial class MainWindow : Window
#region #region
private void OnFilePageButtonClick(object sender, RoutedEventArgs e) => menu.OpenFilePage();
private void OnFilePageButtonClick(object sender, RoutedEventArgs e) private void OnConfigPageButtonClick(object sender, RoutedEventArgs e) => menu.OpenConfigPage();
{ private void OnStandardPageButtonClick(object sender, RoutedEventArgs e) => menu.OpenStandardPage();
WindowHelper.ShowPageDialog(new FilePage(), "记录列表", 810, 600,isResizable: false, IconHelper.GetIconPath("main1-File.ico")); 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();
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"));
}
#endregion #endregion
#region #region
/// <summary> /// <summary>
/// 按钮点击触发关闭页面 /// 按钮点击触发关闭页面

11
Services/ChartManager.cs Normal file
View File

@ -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
{
}

View File

@ -18,7 +18,7 @@ namespace WpfApp.Utils
iconFileName ??= defaultIconName; iconFileName ??= defaultIconName;
// 默认项目图标目录(相对于 EXE // 默认项目图标目录(相对于 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); string path = SearchDirectory(projectIconDir, iconFileName);
@ -40,6 +40,34 @@ namespace WpfApp.Utils
throw new FileNotFoundException($"找不到图标文件: {iconFileName}"); throw new FileNotFoundException($"找不到图标文件: {iconFileName}");
} }
/// <summary>
/// 获取图标目录路径(智能推断)
/// </summary>
/// <param name="externalDirs">可选外部搜索目录</param>
/// <returns>图标目录的绝对路径</returns>
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}");
}
/// <summary> /// <summary>
/// 在指定目录及子目录搜索文件 /// 在指定目录及子目录搜索文件
/// </summary> /// </summary>

View File

@ -0,0 +1,85 @@
using System.Windows;
using WpfApp.src.view;
using WpfApp.Utils;
namespace WpfApp.src.controllers
{
/// <summary>
/// 菜单控制器 - 统一管理菜单打开逻辑
/// </summary>
public class MenuController
{
private readonly Window mainWindow;
public MenuController(Window window)
{
mainWindow = window;
}
/// <summary>
/// 打开文件记录列表页面
/// </summary>
public void OpenFilePage()
{
WindowHelper.ShowPageDialog(new FilePage(),
"记录列表", 810, 600,
isResizable: false,
IconHelper.GetIconPath("main1-File.ico"));
}
/// <summary>
/// 打开配置页面
/// </summary>
public void OpenConfigPage()
{
WindowHelper.ShowPageDialog(new ConfigPage(),
"配置", 972, 648,
isResizable: false,
IconHelper.GetIconPath("main2-Config.ico"));
}
/// <summary>
/// 打开主标定页面
/// </summary>
public void OpenStandardPage()
{
WindowHelper.ShowPageDialog(new StandardPage(),
"主标定", 972, 648,
isResizable: true,
IconHelper.GetIconPath("main3-Standard.ico"));
}
/// <summary>
/// 打开 Gauge R&R 页面
/// </summary>
public void OpenGaugePage()
{
WindowHelper.ShowPageDialog(new GaugePage(),
"Gauge R&&R", 1080, 720,
isResizable: false,
IconHelper.GetIconPath("main4-Gauge.ico"));
}
/// <summary>
/// 打开 CgCgk 页面
/// </summary>
public void OpenCgCgkPage()
{
WindowHelper.ShowPageDialog(new CgCgkPage(),
"CgCgk", 972, 648,
isResizable: true,
IconHelper.GetIconPath("main5-CgCgk.ico"));
}
/// <summary>
/// 打开系统设置页面
/// </summary>
public void OpenSysSetPage()
{
WindowHelper.ShowPageDialog(new SysSetPage(),
"系统设置", 810, 600,
isResizable: false,
IconHelper.GetIconPath("main6-SysSet.ico"));
}
}
}

View File

@ -18,29 +18,17 @@ public partial class CgCgkPage : Page
private void OnCgCgkPageButtonClick(object sender, RoutedEventArgs e) private void OnCgCgkPageButtonClick(object sender, RoutedEventArgs e)
{ {
string templatePath = @"C:\Users\yongye\Desktop\code\code\SolartronMetrologyWpfApp\WpfApp\src\public\Excel\demo.xlsx"; ExcelHelper.LoadTemplate(IconHelper.GetIconPath("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.SelectSheet("表2"); ExcelHelper.SelectSheet("表2");
// 3⃣ 插入数据
ExcelHelper.InsertRowData(5, 0, new List<object> { "传感器1", 25.6, 101.3, DateTime.Now }); ExcelHelper.InsertRowData(5, 0, new List<object> { "传感器1", 25.6, 101.3, DateTime.Now });
// 4⃣ 修改单元格
ExcelHelper.WriteCell("B2", "测试人员"); ExcelHelper.WriteCell("B2", "测试人员");
ExcelHelper.WriteCell("C3", DateTime.Now.ToString("yyyy-MM-dd")); ExcelHelper.WriteCell("C3", DateTime.Now.ToString("yyyy-MM-dd"));
ExcelHelper.SaveAs(IconHelper.GetIconDirectoryPath() + "\\Excel\\Report_2025_10_13.xlsx");
// 5⃣ 保存为新文件 // 在页面控件上显示保存后的 Excel
ExcelHelper.SaveAs(savePath); grid.Load(IconHelper.GetIconPath("Report_2025_10_13.xlsx"));
// 6⃣ 在页面控件上显示保存后的 Excel // 不要 Dispose否则控件被销毁
grid.Load(savePath); // 这里直接加载 Report_2025_10_13.xlsx
// 7⃣ 不要 Dispose否则控件被销毁
// ExcelHelper.Dispose(); // ExcelHelper.Dispose();
} }

View File

@ -1,20 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; 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.Navigation;
using System.Windows.Shapes; namespace WpfApp.src.view;
namespace WpfApp.src.view
{
/// <summary> /// <summary>
/// FilePage.xaml 的交互逻辑 /// FilePage.xaml 的交互逻辑
/// </summary> /// </summary>
@ -150,4 +138,3 @@ namespace WpfApp.src.view
} }
} }
} }
}