多项功能优化

This commit is contained in:
GG Z
2024-12-22 10:26:12 +08:00
parent 77655c9ef5
commit 83b846f15f
66 changed files with 5424 additions and 2927 deletions

View File

@@ -9,7 +9,7 @@ namespace Sai.Toolkit.Core.Helpers
/// <summary>
/// 获取项目文件夹下的配置文件根据Key获取Value
/// </summary>
/// <param name="key"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetCfgValue(string key)
{
@@ -25,8 +25,8 @@ namespace Sai.Toolkit.Core.Helpers
/// <summary>
/// 保存到程序目录下的配置文件
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SetCfgPairs(string key, string value)
{
var assembly = Assembly.GetCallingAssembly();

View File

@@ -6,170 +6,14 @@ namespace Sai.Toolkit.Core.Helpers;
public static class EnumHelpers
{
/// <summary>
/// 获取当前枚举的值和枚举描述
/// </summary>
/// <returns></returns>
public static List<KeyValuePair<int, string>> GetAll<T>()
where T : IComparable, IFormattable, IConvertible
{
var list = new List<KeyValuePair<int, string>>();
foreach (var field in typeof(T).GetFields())
{
if (field.FieldType.IsEnum)
{
var tmp = field.GetValue(null);
var enumValue = (Enum)tmp;
var intValue = Convert.ToInt32(enumValue);
var showName = enumValue.GetDescription();
list.Add(new KeyValuePair<int, string>(intValue, showName));
}
}
return list;
}
/// <summary>
/// 获取当前枚举值的描述,没有描述则返回空字符串
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetDescription(this Enum value)
{
var description = string.Empty;
var fieldInfo = value.GetType().GetField(value.ToString());
var attr = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
if (attr != null)
{
description = attr.Description;
}
return description;
}
public static Dictionary<string, T> GetDescriptionEnumMap<T>()
where T : Enum
{
var results = new Dictionary<string, T>();
var values = Enum.GetValues(typeof(T));
foreach (T value in values)
{
var member = typeof(T).GetMember(value.ToString()).FirstOrDefault();
if (member is null)
{
continue;
}
var customAttributes = member.GetCustomAttributes(typeof(DescriptionAttribute), false);
var name = (customAttributes.FirstOrDefault() as DescriptionAttribute)?.Description;
if (name != null)
{
results.Add(name, value);
}
}
return results;
}
public static Dictionary<string, T> GetDisplayEnumDict<T>()
where T : Enum
{
var results = new Dictionary<string, T>();
var values = Enum.GetValues(typeof(T));
foreach (T value in values)
{
var member = typeof(T).GetMember(value.ToString()).FirstOrDefault();
if (member is null)
{
continue;
}
var atts = member.GetCustomAttributes(typeof(DisplayAttribute), false);
var name = (atts.FirstOrDefault() as DisplayAttribute)?.Name;
if (name != null)
{
results.Add(name, value);
}
}
return results;
}
/// <summary>
/// 获取描述的扩展方法
/// </summary>
/// <param name="enumObj"></param>
/// <param name="value">枚举值</param>
/// <returns></returns>
public static string GetEnumDescription(this Enum enumObj, int? value)
{
var fields = enumObj.GetType().GetFields();
for (int i = 1, count = fields.Length; i < count; i++)
{
if ((int)Enum.Parse(enumObj.GetType(), fields[i].Name) == value)
{
var enumAttributes = (DescriptionAttribute[])fields[i].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (enumAttributes.Length > 0)
{
return enumAttributes[0].Description;
}
}
}
return string.Empty;
}
private static void GetInfo(Type enumType)
{
_ = Enum.GetValues(enumType); //得到的是枚举类型,用于遍历枚举类
_ = Enum.GetNames(enumType); //得到的是枚举类的字符串
}
public static List<string> TraverseEnum<T>()
{
List<string> pName = new();
foreach (T p in Enum.GetValues(typeof(T)))
{
pName.Add(p.ToString());
}
return pName;
}
public static bool TryParseByDescAttr<T>(string value, out T result)
where T : Enum
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
var descs = GetDescriptionEnumMap<T>();
return !descs.TryGetValue(value, out result) ? throw new InvalidEnumArgumentException() : true;
}
public static bool TryParseByDisplayAttr<T>(string value, out T result)
where T : Enum
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
var dict = GetDisplayEnumDict<T>();
return !dict.TryGetValue(value, out result) ? throw new InvalidEnumArgumentException() : true;
}
/// <summary>
/// 获取当前枚举值的描述,没有描述则返回空字符串
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static T GetAttribute<T>(this Enum value) where T : Attribute
{
var fieldInfo = value.GetType().GetField(value.ToString());
return fieldInfo.GetCustomAttribute<T>();
}
}

View File

@@ -17,11 +17,12 @@ public static class WinDialogHelper
/// <param name="filterName">过滤器的名称</param>
/// <param name="extensions">仅扩展名,*代表所有文件</param>
/// <returns></returns>
public static string CreateFilter(string filterName, params string[] extensions)
public static FileDialog SetFilter(this FileDialog dialog, string filterName, params string[] extensions)
{
if (extensions[0] == "*")
{
return "所有文件(*)|*";
dialog.Filter = "所有文件(*)|*";
//return "所有文件(*)|*";
}
var str = string.Empty;
@@ -34,8 +35,9 @@ public static class WinDialogHelper
str += ";";
}
}
return $"{filterName}({str})|{str}";
dialog.Filter = $"{filterName}({str})|{str}";
return dialog;
//return $"{filterName}({str})|{str}";
}
/// <summary>
@@ -50,9 +52,10 @@ public static class WinDialogHelper
new()
{
CheckFileExists = true,
Filter = CreateFilter(title, extensions),
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
};
dialog.SetFilter(title, extensions);
return dialog.ShowDialog() == true ? dialog.FileName : null;
}
@@ -93,6 +96,7 @@ public static class WinDialogHelper
public static void ShowModeless<T>(ObservableObject viewModel)
where T : Window, new()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
var view = SingletonViewHelper<T>.GetInstance(out var isNewCreate);
if (isNewCreate)
{
@@ -100,6 +104,8 @@ public static class WinDialogHelper
view.ShowAhead();
}
view.Activate();
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomainOnAssemblyResolve;
//AssemblyLoaderHelpers loaderUtil = new(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
//loaderUtil.HookAssemblyResolve();
//try
@@ -121,6 +127,19 @@ public static class WinDialogHelper
// loaderUtil.UnhookAssemblyResolve();
//}
}
private static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
{
if (!args.Name.Contains("Xaml.Behaviors"))
{
return null;
}
var assemblyFile = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"Microsoft.Xaml.Behaviors.dll"
);
return File.Exists(assemblyFile) ? Assembly.LoadFrom(assemblyFile) : null;
}
private static Dictionary<Type, Window> _windows = [];
public static void ShowOrActivate<TWindow, TViewModel>(params object[] viewModelParams)
where TWindow : Window, new()