diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
index 2ce832d..d2c7c6f 100644
--- a/MainWindow.xaml.cs
+++ b/MainWindow.xaml.cs
@@ -85,32 +85,32 @@ public partial class MainWindow : Window
private void OnFilePageButtonClick(object sender, RoutedEventArgs e)
{
- WindowHelper.ShowPageDialog(new FilePage(), "记录列表", 810, 600,isResizable: false, "..\\..\\..\\src\\public\\Icons\\programIcons\\main1-File.ico");
+ 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, "..\\..\\..\\src\\public\\Icons\\programIcons\\main2-Config.ico");
+ 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, "..\\..\\..\\src\\public\\Icons\\programIcons\\main3-Standard.ico");
+ 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, "..\\..\\..\\src\\public\\Icons\\programIcons\\main4-Gauge.ico");
+ 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, "..\\..\\..\\src\\public\\Icons\\programIcons\\main5-CgCgk.ico");
+ 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, "..\\..\\..\\src\\public\\Icons\\programIcons\\main6-SysSet.ico");
+ WindowHelper.ShowPageDialog(new SysSetPage(), "系统设置", 810, 600, isResizable:false, IconHelper.GetIconPath("main6-SysSet.ico"));
}
#endregion
diff --git a/Utils/IconHelper.cs b/Utils/IconHelper.cs
new file mode 100644
index 0000000..eed5cd0
--- /dev/null
+++ b/Utils/IconHelper.cs
@@ -0,0 +1,58 @@
+using System;
+using System.IO;
+
+namespace WpfApp.Utils
+{
+ public static class IconHelper
+ {
+ ///
+ /// 获取图标路径(智能搜索)
+ ///
+ /// 图标文件名,例如 "FMSDGAUGE-main.ico",为空使用默认图标
+ /// 可选外部搜索目录
+ /// 图标绝对路径
+ public static string GetIconPath(string iconFileName = null, string[] externalDirs = null)
+ {
+ // 默认图标文件名
+ string defaultIconName = "FMSDGAUGE-main.ico";
+ iconFileName ??= defaultIconName;
+
+ // 默认项目图标目录(相对于 EXE)
+ string projectIconDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\src\public\Icons");
+
+ // 先搜索项目目录
+ string path = SearchDirectory(projectIconDir, iconFileName);
+ if (!string.IsNullOrEmpty(path))
+ return path;
+
+ // 再搜索外部目录
+ if (externalDirs != null)
+ {
+ foreach (var dir in externalDirs)
+ {
+ path = SearchDirectory(dir, iconFileName);
+ if (!string.IsNullOrEmpty(path))
+ return path;
+ }
+ }
+
+ // 都没找到就抛异常
+ throw new FileNotFoundException($"找不到图标文件: {iconFileName}");
+ }
+
+ ///
+ /// 在指定目录及子目录搜索文件
+ ///
+ private static string SearchDirectory(string rootDir, string fileName)
+ {
+ if (string.IsNullOrEmpty(rootDir) || !Directory.Exists(rootDir))
+ return null;
+
+ var files = Directory.GetFiles(rootDir, fileName, SearchOption.AllDirectories);
+ if (files.Length > 0)
+ return Path.GetFullPath(files[0]);
+
+ return null;
+ }
+ }
+}
diff --git a/Utils/WindowHelper.cs b/Utils/WindowHelper.cs
index 4bfa061..7c4dfd1 100644
--- a/Utils/WindowHelper.cs
+++ b/Utils/WindowHelper.cs
@@ -31,20 +31,16 @@ namespace WpfApp.Utils
Owner = Application.Current.MainWindow,
ResizeMode = isResizable ? ResizeMode.CanResize : ResizeMode.NoResize
};
- string defaultIconPath = null;
- if (iconPath == null)
- defaultIconPath = @"..\..\..\src\public\Icons\programIcons\FMSDGAUGE-main.ico";
- else
- defaultIconPath = iconPath;
+ string defaultIconPath = iconPath == null ? IconHelper.GetIconPath("FMSDGAUGE-main.ico") : iconPath;
- try
- {
- navWin.Icon = new BitmapImage(new Uri(System.IO.Path.GetFullPath(defaultIconPath), UriKind.Absolute));
- }
- catch (Exception ex)
- {
- Console.WriteLine($"设置图标失败: {ex.Message}");
- }
+ try
+ {
+ navWin.Icon = new BitmapImage(new Uri(System.IO.Path.GetFullPath(defaultIconPath), UriKind.Absolute));
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"设置图标失败: {ex.Message}");
+ }
navWin.ShowDialog();