56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sinvo.EplanHpD.Plugin.WPFUI.Utils
|
|
{
|
|
/// <summary>
|
|
/// 消息发送与接收
|
|
/// </summary>
|
|
public class MessageSend
|
|
{
|
|
private static readonly Dictionary<string, List<Action<object>>> _subscribers = new();
|
|
|
|
public static void Subscribe(string message, Action<object> callback)
|
|
{
|
|
if (!_subscribers.ContainsKey(message))
|
|
{
|
|
_subscribers[message] = new List<Action<object>>();
|
|
}
|
|
_subscribers[message].Add(callback);
|
|
}
|
|
|
|
public static void Unsubscribe(string message, Action<object> callback)
|
|
{
|
|
if (_subscribers.ContainsKey(message))
|
|
{
|
|
_subscribers[message].Remove(callback);
|
|
if (_subscribers[message].Count == 0)
|
|
{
|
|
_subscribers.Remove(message);
|
|
}
|
|
}
|
|
}
|
|
public static void Unsubscribe(string message)
|
|
{
|
|
if (_subscribers.ContainsKey(message))
|
|
{
|
|
_subscribers.Remove(message);
|
|
}
|
|
}
|
|
|
|
public static void Publish(string message, object parameter = null)
|
|
{
|
|
if (_subscribers.ContainsKey(message))
|
|
{
|
|
foreach (var callback in _subscribers[message])
|
|
{
|
|
callback(parameter);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|