首先从一个场景说起:
我们常常有这样一个需求,比如说在列表窗体(Master)有很多按钮,如增加,修改,删除,保存,取消等,不同的情况下,按钮的状态可能会不一样,比如说,只有在编辑状态时,保存,取消按钮才可用等等.
然后双击列表,会弹出一个明细窗体(Detail),这个窗体也有如增加,修改,删除,保存,取消等按钮,且功能和刷新条件也与Master窗体的功能差不多,我们常把各自的业务逻辑写在各自的窗体,但这样不是显得太冗余了吗,其实,我们有一种比较好的方法来解决这个问题.
用过DELPHI的人应该都知道,有个TACTION类解决的就是这个问题,下面我用.NET代码来模拟TACTION的实现:
1. 先设计一个命令Cmd类,主要定义操作的行为(如执行,刷新)及界面的绑定(此处假定绑定类为ToolStripMenuItem,如需绑定其它类,请自己扩展).其实现代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
internal class Cmd
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
private List<ToolStripMenuItem> cmds = new List<ToolStripMenuItem>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string Text = null;
private Image Image = null;
private Keys ShortKey = 0;
private EventHandler CallMethod = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
internal Cmd(string Text, Keys ShortKey, Image Image, EventHandler CallMethod)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.Text = Text;
this.ShortKey = System.Windows.Forms.Keys.Control | ShortKey;
this.Image = Image;
this.CallMethod = CallMethod;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
internal void Refresh(bool IsEnabled)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
foreach (ToolStripMenuItem cmd in cmds)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cmd.Enabled = IsEnabled;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
internal void AddBind(ToolStripMenuItem mnu)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (this.cmds.Contains(mnu)) return;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
mnu.Text = this.Text;
mnu.Image = this.Image;
if (this.ShortKey != 0) mnu.ShortcutKeys = this.ShortKey;
mnu.Click += CallMethod;
this.cmds.Add(mnu);
}
}
2. 设计一个Act类,对Cmd进行封装
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
public class Act
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
private Dictionary<string, Cmd> cmds = new Dictionary<string, Cmd>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void AddAction(string Name, string Text, Keys ShortKey, Image Image, EventHandler CallMethod)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (cmds.ContainsKey(Name)) cmds.Remove(Name);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Cmd cmd = new Cmd(Text, ShortKey, Image, CallMethod);
cmds.Add(Name, cmd);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void AddBind(string Name, ToolStripMenuItem mnu)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (mnu == null) return;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!cmds.ContainsKey(Name)) throw new Exception("未设置名称为[" + Name + "]的动作,不能绑定!");
cmds[Name].AddBind(mnu);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Refresh(string Name, bool IsEnabled)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (!cmds.ContainsKey(Name)) throw new Exception("未设置名称为[" + Name + "]的动作,不能刷新!");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cmds[Name].Refresh(IsEnabled);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 public class Act
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
3 private Dictionary<string, Cmd> cmds = new Dictionary<string, Cmd>();
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5 public void AddAction(string Name, string Text, Keys ShortKey, Image Image, EventHandler CallMethod)
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
7 if (cmds.ContainsKey(Name)) cmds.Remove(Name);
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9 Cmd cmd = new Cmd(Text, ShortKey, Image, CallMethod);
10 cmds.Add(Name, cmd);
11 }
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13 public void AddBind(string Name, ToolStripMenuItem mnu)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
15 if (mnu == null) return;
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17 if (!cmds.ContainsKey(Name)) throw new Exception("未设置名称为[" + Name + "]的动作,不能绑定!");
18 cmds[Name].AddBind(mnu);
19 }
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21 public void Refresh(string Name, bool IsEnabled)
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
23 if (!cmds.ContainsKey(Name)) throw new Exception("未设置名称为[" + Name + "]的动作,不能刷新!");
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25 cmds[Name].Refresh(IsEnabled);
26 }
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28 }
3. 效果演示
增加master,entry窗体,随便增加一些按钮,命好名就行了
![](https://www.cnblogs.com/images/cnblogs_com/yaozy/111.JPG)
![](https://www.cnblogs.com/images/cnblogs_com/yaozy/222.JPG)
增加master的Action动作及绑定代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Test
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
public partial class Form1 : Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
MZ.CT.Act act = new MZ.CT.Act();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Form1()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
InitializeComponent();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//定义动作行为
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) act.AddAction("Add", "增加", Keys.A, null, delegate { this.Refresh(true); });
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) act.AddAction("Edit", "修改", Keys.E, null, delegate { this.Refresh(true); });
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) act.AddAction("Delete", "删除", Keys.D, null, delegate { this.Refresh(true); });
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) act.AddAction("Save", "保存", Keys.S, null, delegate { this.Refresh(false); });
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) act.AddAction("Cancel", "取消", Keys.C, null, delegate { this.Refresh(false); });
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
act.AddAction("Show", "打开子窗体", Keys.S, null,
delegate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
using (Form2 frm = new Form2(act))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
frm.ShowDialog();
}
}
);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//绑定动作
act.AddBind("Add", this.btnAdd);
act.AddBind("Edit", this.btnEdit);
act.AddBind("Delete", this.btnDelete);
act.AddBind("Save", this.btnSave);
act.AddBind("Cancel", this.btnCancel);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
act.AddBind("Show", this.btnShow);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Refresh(bool IsEnabled)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.act.Refresh("Add", !IsEnabled);
this.act.Refresh("Edit", !IsEnabled);
this.act.Refresh("Delete", !IsEnabled);
this.act.Refresh("Save", IsEnabled);
this.act.Refresh("Cancel", IsEnabled);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
增加entry绑定代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Test
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
public partial class Form2 : Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
public Form2(MZ.CT.Act act)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
InitializeComponent();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//重复绑定
act.AddBind("Add", this.btnAdd);
act.AddBind("Edit", this.btnEdit);
act.AddBind("Delete", this.btnDelete);
act.AddBind("Save", this.btnSave);
act.AddBind("Cancel", this.btnCancel);
}
}
}
最终效果演示:
master中点击增加后的刷新效果:
![](https://www.cnblogs.com/images/cnblogs_com/yaozy/333.JPG)
打开entry后,联动的刷新效果:
![](https://www.cnblogs.com/images/cnblogs_com/yaozy/444.JPG)
由于本人比较忙且太过懒散,很久没有更新BLOG了,过年时如果有时间的话,我准备写一系列分布式框架设计的文章,望广大园友支持及指正!
posted on
2006-12-22 11:33
一万光年外
阅读( 2115)
评论()
编辑
收藏
举报
|