我试图找到创建系统的最佳方法,即可以将事件源添加到管理器类,然后将其重新分派给侦听器。具体来说,我有许多不同的输入源(键盘输入源,鼠标输入源,虚拟键盘输入源等),我希望允许开发人员监听键盘输入源和输入上的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
好点。我会检查出来的! –