您的位置:首页 > 编程语言 > C#

C#一步一步实现插件框架的示例(一)

2013-01-19 00:23 423 查看
像我这样的菜鸟,写程序一般就是拖控件,双击,然后写上执行的代码,这样在窗口中就有很多事件代码,如果要实现各按钮的状态,那得在很多地方修改代码,极为复杂.通过参考CSHARPDEVELOP的代码就说明和网上各位朋友的示例,在这里,自己实现了一个很简单的插件程序,方便程序的开发,每个功能可以独立开发,也方便维护.现在给大家讲讲其方法.先上张图片:



由于我这个插件使用了DEVEXPRESS的控件,所有要运行就需要安装,在这里我们就不使用DEV的控件了.

首先,我们需要定义一个接口,该接口定义了一个工具栏的按钮要执行的动作:ICommnd

public  interface ICommand
{
void Run();
}


接着我们申明一个Abstract的类:

public abstract class AbstractCommand:ICommand
{
public abstract void Run();
public virtual bool IsEnable { get; set; }
public virtual string Caption { get; set; }
}


在本抽象类中我们增加了两个虚的属性,一个代表该按钮的状态,另一个是按钮的标题.后面我们在

说如很使用.

再申明一个接口:IStatusUpdate ,用于规定按钮更新状态和标题的方法

public interface IStatusUpdate
{
void UpdateStatus();
void UpdateText();
}


再申明一有类:ToolBarButton,本类继承于工具栏类和IStatusUpdate接口 ,我们使用该类根据

AbstractCommand的子类来生成对应的按钮.代码如下:

public partial class ToolBarButton :ToolStripButton,IStatusUpdate
{
AbstractCommand info = null;
//提供了一个构造函数,需要传入AbstractCommand的子类,子类包含了按钮所要执行的功能和其标题、状态.
public ToolBarButton(AbstractCommand info)
{
this.info = info;
this.Text = info.Caption;
this.Enabled = info.IsEnable;
this.DisplayStyle = ToolStripItemDisplayStyle.Text;
this.Click += new EventHandler(ToolBarButton_Click);
}
void ToolBarButton_Click(object sender, EventArgs e)
{//在该按钮被按下后执行AbstractCommand子类的Run函数
if (this.info != null) info.Run();
}
public void UpdateStatus()
{//更新其状态
if (this.info != null) this.Enabled = info.IsEnable;
}
public void UpdateText()
{//更新其标题
if (this.info != null) this.Text = info.Caption;
}
}
}


当我们要使用该框架来生成一个按钮时只需要申明一类并继承于:AbstractCommand类就可以了:

public class test:AbstractCommand
{
public override void Run()
{
MessageBox.Show("Test");
}
public override bool IsEnable
{
get
{//该按钮对应状态的变化,我们是根据判断C:\\1.TXT是否存在来确定的.
return System.IO.File.Exists("c:\\1.txt");
}
set
{
base.IsEnable = value;
}
}
public override string Caption
{
get
{
return "Test";
}
set
{
base.Caption = value;
}
}
}


现在我们的程序还没有主窗口,我们需要增加一个主窗口,在这里为了简单,直接将生成按钮的代码写在了里面,在实际应用时,使用反射将其生成.

前面写了要更新按钮的状态,但是并没有地方调用了该函数,所以在主窗口中增加了一个事件,在

该事件中更新其状态: Application.Idle += new EventHandler(Application_Idle);

void Application_Idle(object sender, EventArgs e)
{
this.UpdateToolBarButtonSatus();
}
public void UpdateToolBarButtonSatus()
{
foreach (var t in list)
{
t.UpdateStatus();
t.UpdateText();
}
}


在这就只实现了增加一个按钮,当然你可自己再仿照test类再写一个类,并在workbench类中继续增加其实例,即可要增加按钮.

下一篇我们对其进行改造,让其通过反射来动态增加按钮.这样我们就可以不用重新编译主窗口,要增加一个按钮只需要生成一个类然后编译成一个DLL 就可以了.

最后附上完整代码:

http://files.cnblogs.com/city-hunter/ExampleAddin.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: