我在使用bellow類的C#應用程序中實現中介模式。我注意到,如果我有多個訂閱相同事件的訂閱者(對象),只有其中一個接收它。這是一個正常的行爲?我怎麼能達到他們都接受它?調解器模式問題
由此引發事件
Mediator.NotifyColleagues("SetMyProject", MyProject);
多個對象(類)通過波紋線
Mediator.Register("SetMyProject", SetMyProject);
中保類
static public class Mediator
{
static IDictionary<string, List<Action<object>>> pl_dict = new Dictionary<string, List<Action<object>>>();
static public void Register(string token, Action<object> callback)
{
if (!pl_dict.ContainsKey(token))
{
var list = new List<Action<object>>();
list.Add(callback);
pl_dict.Add(token, list);
}
else
{
bool found = false;
foreach (var item in pl_dict[token])
if (item.Method.ToString() == callback.Method.ToString())
found = true;
if (!found)
pl_dict[token].Add(callback);
}
}
static public void Unregister(string token, Action<object> callback)
{
if (pl_dict.ContainsKey(token))
pl_dict[token].Remove(callback);
}
static public void NotifyColleagues(string token, object args)
{
if (pl_dict.ContainsKey(token))
foreach (var callback in pl_dict[token])
callback(args);
}
}
我不知道爲什麼會影響其他receipents接收消息 – Jim
因爲'行動
Acutally這個人做了訣竅...將保留當前項目的這個解決方案,但將移動到棱鏡eventagregator – Jim