我試圖找到創建系統的最佳方法,即可以將事件源添加到管理器類,然後將其重新分派給偵聽器。具體來說,我有許多不同的輸入源(鍵盤輸入源,鼠標輸入源,虛擬鍵盤輸入源等),我希望允許開發人員監聽鍵盤輸入源和輸入上的KeyDown事件經理本身(從任何活動輸入源捕捉此事件)。彙總事件源和重新分派事件的最佳方式
在我最終創建許多「調度」功能時,很容易強行推行一個解決方案,只需在它們經過時重新分派事件,但最終我會產生許多單行功能,並且必須創建新的無論何時將新事件添加到輸入源接口時都會起作用。
我已經考慮過使用lambdas,但我需要一種方法來解除事件,如果輸入源從經理中刪除。我可以將lambda保存在一個字典中,由輸入源鍵入,但許多事件具有不同的arg類,創建多個字典會導致難看。
我想知道如果我錯過了一些簡單的方法來做到這一點,使事情保持乾淨,並保持我需要記下的額外代碼的數量。
僅供參考,這裏是我的工作對象的示例:
public interface IInputSource {}
public interface IKeyboardInputSource : IInputSource
{
event EventHandler<KeyboardEventArgs> KeyDown;
event EventHandler<KeyboardEventArgs> KeyUp;
}
public interface IMouseInputSource : IInputSource
{
event EventHandler<MouseEventArgs> MouseDown;
event EventHandler<MouseEventArgs> MouseUp;
}
public class InputManager : IKeyboardInputSource, IMouseInputSource
{
private List<IInputSource> InputSources;
//Event declarations from IKeyboardInputSource and IMouseInputSource
public void AddSource(IInputSource source)
{
InputSources.Add(source);
if (source is IKeyboardInputSource)
{
var keyboardSource = source as IKeyboardInputSource;
keyboardSource.KeyDown += SendKeyDown;
// Listen for other keyboard events...
}
if (source is IMouseInputSource)
{
// Listen for mouse events...
}
}
public void RemoveSource(IInputSource source)
{
if (source is IKeyboardInputSource)
{
var keyboardSource = source as IKeyboardInputSource;
keyboardSource.KeyDown -= SendKeyDown;
// Remove other keyboard events...
}
if (source is IMouseInputSource)
{
// Remove mouse events...
}
InputSources.Remove(source);
}
private void SendKeyDown(object sender, KeyboardEventArgs e)
{
if (KeyDown != null)
KeyDown(sender, e);
}
//Other "send" functions
}
我應該提到這前面 - 但我工作的項目不允許我使用任何第三方庫,很遺憾。 –
但是,您可以檢查bbvcommon庫中使用的代碼,因爲它是開源的,然後您可以將其複製並將其修改爲您的代碼。這是直接鏈接到源代碼https://github.com/bbvcommon/bbv.Common – Jupaol
好點。我會檢查出來的! –