You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.2 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
//using DQ.Construction.NewLook.Utility;
using WorkData;
using WorkData.Entity;
#region 文件说明
/*-----------------------------------------------------
* 版权所有 c 2024 jdfcd
* CLR版本 4.0.30319.42000
* 命名空间 DQ.Construction.NewLook.DataManager
*
* 创建者pnpe
* 电子邮件pnpe@qq.com
* 创建时间2024/6/19 12:01:26
*/
#endregion 文件说明
namespace WellWorkDataUI
{
/// <summary>
/// The k e p data manager.
/// </summary>
public static class KEPDataManager
{
// 使用字典来存储不同类型的事件
private static Dictionary<Type, Dictionary<string, Action>> subscriptions = new Dictionary<Type, Dictionary<string, Action>>();
/// <summary>
/// 基础
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <param name="callback"></param>
/// <param name="loadCallBack"></param>
private static void Subscribe<T>(string name, Action callback, bool loadCallBack = false)
where T : class
{
string pageUniqueValue = name;
var type = typeof(T);
if (!subscriptions.ContainsKey(type))
{
subscriptions[type] = new Dictionary<string, Action>();
}
if (!subscriptions[type].ContainsKey(pageUniqueValue))
{
subscriptions[type].Add(pageUniqueValue, callback);
}
else
{
subscriptions[type][pageUniqueValue] = callback;
}
if (loadCallBack)
{
NotifyChange<T>();
}
}
public static void Subscribe<T>(this Form form, Action callback, bool loadCallBack = false)
where T : class
{
Subscribe<T>(form.Name, callback, loadCallBack);
}
/// <summary>
/// 订阅方法
/// </summary>
/// <param name="customUserControl">The page unique value.</param>
/// <param name="callback">The callback.</param>
/// <param name="loadCallBack">If true, load call back.</param>
//public static void Subscribe<T>(this CustomUserControl customUserControl, Action callback, bool loadCallBack = false)
// where T : class
//{
// Subscribe<T>(customUserControl.Name, callback, loadCallBack);
//}
///// <summary>
///// 订阅方法
///// </summary>
///// <param name="customUserControl">The page unique value.</param>
//public static void UnSubscribe<T>(this CustomUserControl customUserControl)
// where T : class
//{
// string pageUniqueValue = customUserControl.Name;
// var type = typeof(T);
// if (!subscriptions.ContainsKey(type))
// {
// subscriptions[type] = new Dictionary<string, Action>();
// }
// if (subscriptions[type].ContainsKey(pageUniqueValue))
// {
// subscriptions[type].Remove(pageUniqueValue);
// }
//}
// 触发事件的方法
/// <summary>
/// Ons the data changed.
/// </summary>
/// <param name="t">The t.</param>
public static void NotifyChange<T>()
where T : class
{
Task.Run(() =>
{
var type = typeof(T);
if (subscriptions.ContainsKey(type))
{
foreach (var subscription in subscriptions[type].Values)
{
try
{
subscription?.Invoke();
}
catch (Exception ex)
{
//Log
Reporter.WriteMsg($"数据管理推送数据:" + ex.Message);
}
}
}
if (type == typeof(WellDeflection))
{
//清理井斜相关缓存
CalcDataHelp.Reset();
}
});
}
}
}