添加菜单控制器
This commit is contained in:
parent
3a11035329
commit
6e14f38a41
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -22,6 +24,7 @@ public partial class MainWindow : Window
|
|||
private readonly LogService log;
|
||||
private readonly DatabaseService db;
|
||||
private readonly EventService<GeneralEventArgs> even;
|
||||
private readonly MenuController menu;
|
||||
|
||||
public MainWindow(LogService logService, DatabaseService databaseService, EventService<GeneralEventArgs> 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 关闭和测试按钮
|
||||
/// <summary>
|
||||
/// 按钮点击触发关闭页面
|
||||
|
|
|
|||
11
Services/ChartManager.cs
Normal file
11
Services/ChartManager.cs
Normal 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
|
||||
{
|
||||
}
|
||||
|
|
@ -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}");
|
||||
}
|
||||
|
||||
/// <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>
|
||||
|
|
|
|||
85
src/controllers/MenuController.cs
Normal file
85
src/controllers/MenuController.cs
Normal 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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
|
@ -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<object> { "传感器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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,25 +1,13 @@
|
|||
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
|
||||
/// <summary>
|
||||
/// FilePage.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class FilePage : Page
|
||||
{
|
||||
/// <summary>
|
||||
/// FilePage.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class FilePage : Page
|
||||
{
|
||||
public FilePage()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
|
@ -149,5 +137,4 @@ namespace WpfApp.src.view
|
|||
Window.GetWindow(this)?.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user