我有兩個類,Input和「EventSystem」。 EventSystem是處理「內部應用程序事件」的內部(非系統相關)類。輸入是一個依賴類(系統相關),處理鍵/按鈕事件並將它們映射到關鍵事件和「EventSystem」。我會告訴你我目前是如何傳遞數據的,它在內部看起來很乾淨,但在外面很髒。有誰知道更好的方法 - 或更簡單的方法 - 傳遞自定義值?向事件系統傳遞自定義數據/值
的EventSystem:
// Raisable Events
public enum EventType { Action, Cancel };
// Base for custom arguments
public class EventArguments
{
public double time;
public EventArguments(double _time)
{
time = _time;
}
}
// Event Delegate (invoked on raise)
public delegate void Event(EventArguments eventArgs);
// full class
static class EventSystem
{
private static Dictionary<EventType, Event> eventMapping = new Dictionary<EventType, Event>();
public static void HookEvent(EventType eventType, Event _event)
{
if (eventMapping.ContainsKey(eventType))
{
eventMapping[eventType] += _event;
}
else
{
eventMapping.Add(eventType, _event);
}
}
public static void RaiseEvent(EventType eventType, EventArguments args)
{
if (eventMapping.ContainsKey(eventType))
{
eventMapping[eventType].Invoke(args);
}
else
{
// do nothing
}
}
}
我輸入ARGS只是繼承EventArguments。
// Inherits EventArguments (double time) and adds it's own, "bool pressed"
class KeyInputArguments : EventArguments
{
public bool pressed;
public KeyInputArguments(double time, bool _pressed) :
base(time)
{
pressed = _pressed;
}
}
當一個鍵被按下它觸發鍵(輸入)的事件,則它檢查以查看是否鍵被映射到內部事件,提高它。一個單獨的類(Config)處理映射/綁定鍵到事件的所有配置。
// Raise on press
EventSystem.RaiseEvent(eventType, new KeyInputArguments(time, true));
// [...]
// Raise on release
EventSystem.RaiseEvent(eventType, new KeyInputArguments(time, false));
最後,爲了觸發事件,我們必須註冊的關鍵事件(這是「外部」代碼)
// Hook our "Enter" function into the Action event
EventSystem.HookEvent(EventType.Action, Enter);
// [...]
public void Enter(EventArguments eventArg)
{
if (((KeyInputArguments)eventArg).pressed == false)
{
Error.Break();
}
}
一切都很好,直到我看到'我的C#和OOP編程知識有限,這是一個難看的結果
我無法更改Enter方法參數,因爲事件委託明確需要EventArgum經濟需求。 (即使KeyInputArguments繼承它?)。我也不明白爲什麼要將eventArg投入到KeyInputArguments。
最後,我也試過這個(雖然我不喜歡那麼多)
KeyInputArguments keyInputArguments = (KeyInputArguments)eventArg;
if (keyInputArguments.pressed == false)
我需要自定義數據的原因是,我打算從多形式的輸入,如遊戲手柄的接收輸入。這意味着我可以將系統相關數據(遊戲手柄設備信息)處理爲獨立參數。這將系統相關數據限制在我的Input類中,同時將我的事件系統作爲獨立的內部使用。 我在做什麼有更好的方法嗎?
你可以看看使用「as」操作符。 http://msdn.microsoft.com/en-us/library/cscsdfbt(v=vs.100).aspx – 2012-08-06 04:52:43
不能直接回答你的問題,但你可以看看棱鏡的事件聚合器,我認爲做你需要什麼http://msdn.microsoft.com/en-us/library/ff921122%28v=pandp.20%29.aspx – 2012-08-06 05:46:43