在项目中引用了微软在智能客户端中用的代码(issuevision)
近来在做项目,可能又要用到这个东西,希望直接将其直接使用。但是最近的项目开始使用Devexpress控件,原有的代码只适用于系统自带控件,而且每增加一种类型的控件需要继承原有类重写代码,想起了泛型于是修改成了下面的代码,现在已经可以适用于所有winform下的系统控件,但是在事件处理这块对第三方控件无效:
效果:已实现winform下控件的“泛型”命令模式,无需在重载原有方法,直接使用
问题:在使用第三方控件时由于事件不一致,需要解决(问题在代码注释当中),希望高手能帮助解决!!!在此先谢谢了
Code
1 using System;
2 namespace CommandApp
3 {
4 public enum enumEvent { Click, ItemClick };
5 public class GenCommander<T>
6 {
7 private T m_genitem;
8 private Command m_command;
9
10 public static void Connect(T item, Command command, enumEvent eventType)
11 {
12 var unused = new GenCommander<T>(item, command, eventType);
13 }
14
15 protected GenCommander(T item,Command command,enumEvent eventType)
16 {
17 try
18 {
19 m_genitem = item;
20 m_command = command;
21 m_command.EnableChanged += HandleEnableChangedEvent;
22
23 System.Reflection.EventInfo eventinfo = typeof(T).GetEvent(eventType.ToString());
24 if (eventinfo != null)
25 {
26 //只对winform控件有效,第三方控件无法使用此方法
27 //报错信息:Object of type 'System.EventHandler' cannot be converted to type 'DevExpress.XtraBars.ItemClickEventHandler'.
28 eventinfo.AddEventHandler(m_genitem, new EventHandler(delegate { m_command.Execute(); }));
29 }
30 }
31 catch (Exception ex)
32 {
33
34 throw ex;
35 }
36 }
37
38
39
40 private void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)
41 {
42 try
43 {
44 System.Reflection.PropertyInfo property = typeof(T).GetProperty("Enabled");
45 if (property != null)
46 {
47 if (property.PropertyType.FullName == "System.Boolean")
48 {
49 property.SetValue(m_genitem, e.IsEnabled, null);
50 }
51 }
52 }
53 catch (Exception ex)
54 {
55
56 throw ex;
57 }
58 }
59 }
60 }
61
1 using System;
2 namespace CommandApp
3 {
4 public enum enumEvent { Click, ItemClick };
5 public class GenCommander<T>
6 {
7 private T m_genitem;
8 private Command m_command;
9
10 public static void Connect(T item, Command command, enumEvent eventType)
11 {
12 var unused = new GenCommander<T>(item, command, eventType);
13 }
14
15 protected GenCommander(T item,Command command,enumEvent eventType)
16 {
17 try
18 {
19 m_genitem = item;
20 m_command = command;
21 m_command.EnableChanged += HandleEnableChangedEvent;
22
23 System.Reflection.EventInfo eventinfo = typeof(T).GetEvent(eventType.ToString());
24 if (eventinfo != null)
25 {
26 //只对winform控件有效,第三方控件无法使用此方法
27 //报错信息:Object of type 'System.EventHandler' cannot be converted to type 'DevExpress.XtraBars.ItemClickEventHandler'.
28 eventinfo.AddEventHandler(m_genitem, new EventHandler(delegate { m_command.Execute(); }));
29 }
30 }
31 catch (Exception ex)
32 {
33
34 throw ex;
35 }
36 }
37
38
39
40 private void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)
41 {
42 try
43 {
44 System.Reflection.PropertyInfo property = typeof(T).GetProperty("Enabled");
45 if (property != null)
46 {
47 if (property.PropertyType.FullName == "System.Boolean")
48 {
49 property.SetValue(m_genitem, e.IsEnabled, null);
50 }
51 }
52 }
53 catch (Exception ex)
54 {
55
56 throw ex;
57 }
58 }
59 }
60 }
61
原有代码:
新旧代码都需要的command类代码:
原有commander代码:
新旧代码都需要的command类代码:
Command类
public class Command
{
public delegate void EnableChangedEventHandler(object sender, EnableChangedEventArgs e);
public virtual event EnableChangedEventHandler EnableChanged;
public delegate void Action();
private Action m_action;
private bool m_isEnabled = true;
public bool IsEnabled
{
get { return m_isEnabled; }
set
{
if (m_isEnabled != value)
{
m_isEnabled = value;
if (EnableChanged != null)
{
EnableChanged(this, new EnableChangedEventArgs(IsEnabled));
}
}
}
}
public Command(Action action)
{
m_action = action;
}
public void Execute()
{
m_action();
}
public class EnableChangedEventArgs : EventArgs
{
private bool m_isEnabled = false;
public bool IsEnabled
{
get { return m_isEnabled; }
}
public EnableChangedEventArgs(bool isEnabled)
{
m_isEnabled = isEnabled;
}
}
}
public class Command
{
public delegate void EnableChangedEventHandler(object sender, EnableChangedEventArgs e);
public virtual event EnableChangedEventHandler EnableChanged;
public delegate void Action();
private Action m_action;
private bool m_isEnabled = true;
public bool IsEnabled
{
get { return m_isEnabled; }
set
{
if (m_isEnabled != value)
{
m_isEnabled = value;
if (EnableChanged != null)
{
EnableChanged(this, new EnableChangedEventArgs(IsEnabled));
}
}
}
}
public Command(Action action)
{
m_action = action;
}
public void Execute()
{
m_action();
}
public class EnableChangedEventArgs : EventArgs
{
private bool m_isEnabled = false;
public bool IsEnabled
{
get { return m_isEnabled; }
}
public EnableChangedEventArgs(bool isEnabled)
{
m_isEnabled = isEnabled;
}
}
}
原有commander代码:
Code
public abstract class Commander
{
protected Command m_command;
protected abstract void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e);
protected Commander(Command command)
{
m_command = command;
m_command.EnableChanged += new Command.EnableChangedEventHandler(HandleEnableChangedEvent);
}
}
//每次增加新控件需要重载Commander抽像类
public class MenuItemCommander : Commander
{
private ToolStripMenuItem m_item;
protected MenuItemCommander(ToolStripMenuItem item, Command command)
: base(command)
{
m_item = item;
m_item.Click += new EventHandler(HandleUIEvent);
}
protected override void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)
{
// m_item.Enabled = e.IsEnabled;
m_item.Visible = e.IsEnabled;
}
private void HandleUIEvent(object sender, EventArgs e)
{
m_command.Execute();
}
public static void Connect(ToolStripMenuItem item, Command command)
{
MenuItemCommander unused = new MenuItemCommander(item, command);
}
}
public class ToolBarButtonCommander : Commander
{
private ToolStripButton m_button;
protected ToolBarButtonCommander(ToolStripButton button, Command command)
: base(command)
{
m_button = button;
button.Click += new EventHandler(button_Click);
}
private void button_Click(object sender, EventArgs e)
{
m_command.Execute();
}
protected override void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)
{
//m_button.Enabled = e.IsEnabled;
m_button.Visible = e.IsEnabled;
}
public static void Connect(ToolStripButton button, Command command)
{
ToolBarButtonCommander unused = new ToolBarButtonCommander(button, command);
}
}
public abstract class Commander
{
protected Command m_command;
protected abstract void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e);
protected Commander(Command command)
{
m_command = command;
m_command.EnableChanged += new Command.EnableChangedEventHandler(HandleEnableChangedEvent);
}
}
//每次增加新控件需要重载Commander抽像类
public class MenuItemCommander : Commander
{
private ToolStripMenuItem m_item;
protected MenuItemCommander(ToolStripMenuItem item, Command command)
: base(command)
{
m_item = item;
m_item.Click += new EventHandler(HandleUIEvent);
}
protected override void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)
{
// m_item.Enabled = e.IsEnabled;
m_item.Visible = e.IsEnabled;
}
private void HandleUIEvent(object sender, EventArgs e)
{
m_command.Execute();
}
public static void Connect(ToolStripMenuItem item, Command command)
{
MenuItemCommander unused = new MenuItemCommander(item, command);
}
}
public class ToolBarButtonCommander : Commander
{
private ToolStripButton m_button;
protected ToolBarButtonCommander(ToolStripButton button, Command command)
: base(command)
{
m_button = button;
button.Click += new EventHandler(button_Click);
}
private void button_Click(object sender, EventArgs e)
{
m_command.Execute();
}
protected override void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)
{
//m_button.Enabled = e.IsEnabled;
m_button.Visible = e.IsEnabled;
}
public static void Connect(ToolStripButton button, Command command)
{
ToolBarButtonCommander unused = new ToolBarButtonCommander(button, command);
}
}