VS中以插件开发的思想开发Winform应用

简单定义:

插件(也称构件)式开发;主要内容就是一个宿主程序加上后期开发的若干插件程序构成整个系统!

宿主程序提供接口注册,插件注册实现接口,从而使不同的插件提供新的功能;

举例:

以下是用VS中的Winform以插件的思想开发的例子;

一:通信接口

  1.打开VS-文件-其他项目类型-VisualStudio解决方案-空白解决方案;

  2.鼠标右键-解决方案-添加-新建项目,选择-类库-命名为:PluginInterface

  3.鼠标右键-类库PluginInterface-添加-新建项-接口-命名为:IShow.cs

  4.IShow.cs中的代码:

using System.Text; 
namespace PluginInterface 
    public interface IShow 
    
      void Show(); 
    
}

  5.鼠标右键-类库PluginInterface-重新生成;

  6.生成后会在该“输出”中看到该接口dll文件的生成路径(该dll文件会在后面新建的宿主程序中引用);

   
   此接口包含了主界面程序(宿主程序)中所包含的所有内容,比如菜单,工具 栏,tabcontrol,tabpage等。

   是所有插件与主界面程序通信的公共契约,该接口包含的仅仅是一些属性,

   而插件就是通过这些属性,与主界面进行交互的。

 

二:主程序(宿主)

  1.鼠标右键-解决方案-添加-新建项目,选择-Windows窗体应用程序-命名为:Mainform
  2.将Mainform下的Form1改名为Mainform;
  3.右键-Mainform下的-引用-添加引用;
  
  4.项目-解决方案-选中PluginInterface-浏览
  
  5.选中dll文件-添加
  
 
  6.Mainform窗体中拖入控件Listbox;
  
  7.Mainform.cs(Mainform窗体后台)中代码如下(注意using PluginInterface;
  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PluginInterface;
using System.Reflection;

namespace MainForm
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();//初始化窗体
            try
            {
                //获取当前程序启动的路径(exe所在路径)
                string path = Application.StartupPath;
                //当前路径中的Plugins目录下(path+Plugins)
                path = System.IO.Path.Combine(path, "Plugins");
                //遍历该目录下所有的dll文件(包含路径)
                foreach (string file in System.IO.Directory.GetFiles(path, "*.dll"))
                {
                    //取最后一个'\'符号后面的1个字符,添加到listBox1集合中
                    listBox1.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
                }

                //+=表示listBox1的Click事件订阅(除了它本身所具有的单机功能外,还可以执行你自己定义的方法)
                //了你自定义的listBox1_Click方法,它会去执行你自定义的方法listBox1_Click()
                listBox1.Click += new EventHandler(listBox1_Click);//委托
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 被订阅的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_Click(object sender, EventArgs e)
        {
            string asmFile = listBox1.SelectedItem.ToString();//获取到当前选中项
            string asmName = System.IO.Path.GetFileNameWithoutExtension(asmFile);
            if (asmFile != string.Empty)
            {
                Assembly a = Assembly.LoadFrom("Plugins\\" + asmFile);
                System.Type[] types = a.GetTypes();
                foreach (System.Type type in types)
                {
                    // 校验加载的 dll 是否实现了契约,当然此处也可以用 Attribute 来实现
                    if (type.GetInterface("IShow") != null)
                    {
                        IShow plugin = (IShow)Activator.CreateInstance(type);
                        plugin.show();
                    }
                }
            }
        }
    }
}
  8.右键-解决方案下的Mainform项目-重新生成,在-输出-中根据该路径找到计算机中对应Mainform.exe所在文件夹,
   在该文件夹下新建Plugins文件夹;
    
  以上便是宿主程序的定义,Plugins文件夹中将要存放的就是后期开发的插件(构件)生成的dll文件,
  以后运行Mainform.exe程序的时候就会检索Plugins文件夹中的插件.dll,实现宿主程序中插件功能的添加

三:插件A(构件A)的开发

  1.鼠标右键-解决方案-添加-新建项目,选择-Windows窗体应用程序-命名为:plugA

  2.将plugA下的Form1改名为plugAForm
  3.应用PluginInterface.DLL文件,与宿主程序中的引用一样;
  4.在plugAForm窗体中添加Lable控件;
  5.plugAForm.cs的后台代码(注意using PluginInterface;)
using System.Text; 
using PluginInterface;
namespace PlugA 
    public class PlugAEnter:IShow 
    
        public void Show() 
        
           new PlusAForm(" 这是 PlugAForm 插件的内容! " ).Show(); 
        
    
      6.右键-解决方案下的plugA项目-重新生成,在-输出-中根据该路径找到计算机中对应的plugA.dll文件;
  7.拷贝plugA.dll文件到Mainform.exe所在文件夹的Plugins文件夹下;

四:插件B(构件B)的开发

  1.与插件A一样,只需将A改成B;

五:运行

  1.在本地资源管理器中找到Mainform.exe,双击运行,
  2.Mainform.exe会到Plugins文件夹下寻找到所有的.dll文件并对其注册,加载到主程序的Listbox中;
  3.点击Listbox框中的PlugA,PlugB即能看到效果!

 

posted on 2016-03-18 11:33  土豆你个马铃薯S  阅读(1039)  评论(0编辑  收藏  举报

导航