随笔 - 8  文章 - 0  评论 - 36  阅读 - 52982

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

像我这样的菜鸟,写程序一般就是拖控件,双击,然后写上执行的代码,这样在窗口中就有很多事件代码,如果要实现各按钮的状态,那得在很多地方修改代码,极为复杂.通过参考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 就可以了.

最后附上完整代码:

https://files.cnblogs.com/city-hunter/ExampleAddin.rar

 

 

 

 

 

posted on   东王  阅读(13537)  评论(12编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2013年1月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示