• winform插件机制学习


         这两天在看自定义控件,原来有太多知识没有掌握。今天看到插件机制,心里突然一亮,这个东西听了不少次,就是不知道是啥回事。这次有幸书里包含一个案例,我就跟着它一步步来。终于知道是什么回事了。这个应该在软件开发中非常多见。只是当时不理解罢了。

    开始

    新建一个winform项目CustomControls
    在窗体上放一个button按钮

    窗体代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    using Plugin;


    namespace CustomControls
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void btnok_Click(object sender, EventArgs e)
    {
    CreatePlugInDockForm("", "Plugin.Form1", "Plugin");
    }

    public static void CreatePlugInDockForm(string strPlugInSubFilePath, string strFormFullName, string strAssemblyName)
    {
    try
    {
    //项目的Assembly选项名称DockSample.dll 即 DockSample
    string path = strAssemblyName;
    //类的全路径名称=> “DockSample.Modules.frm班组日志管理”
    string name = strFormFullName;
    string currentDirectory =
    System.IO.Directory.GetCurrentDirectory();
    if (strPlugInSubFilePath == "")
    {
    currentDirectory = currentDirectory + "\PlugIns";
    }
    else
    {
    //同样的dll,可以按照打上客户的名称,然后加载对应客户的dll,这样就可以防止冲突
    currentDirectory = currentDirectory + "\PlugIns" + "\" +
    strPlugInSubFilePath;
    }
    string[] dllFilenames = System.IO.Directory.GetFiles(currentDirectory,
    strAssemblyName + ".dll");
    if (dllFilenames.Length == 1)
    {
    Assembly asm = Assembly.LoadFrom(dllFilenames[0]);
    Form frm = (Form)asm.CreateInstance(strFormFullName, true);
    //实现PlugIn.ICMFormPlugIn接口
    if (frm.GetType().GetInterface(typeof(ICMFormPlugIn).FullName) != null)
    {
    frm.Show();
    }
    else
    {

    MessageBox.Show("没有实现接口");
    }
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    }
    }

    再新建一个Plugin类库项目
    加一个接口ICMFormPlugIn
    接口代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Plugin
    {
    public interface ICMFormPlugIn
    {
    void Show();
    void Close();
    }
    }

    在里面再新建一个form1窗体,并继承接口。再拖一个按钮控件
    窗体代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace Plugin
    {
    public partial class Form1 : Form,ICMFormPlugIn
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    MessageBox.Show("hello wo shi chajian");
    }
    }
    }

    第一个项目要引用后面插件项目,不然在实现接口调用时错误,这个地方
    //实现PlugIn.ICMFormPlugIn接口
    if (frm.GetType().GetInterface(typeof(ICMFormPlugIn).FullName) != null)
    其实也可以直接写
    frm.GetType().GetInterface("ICMFormPlugIn")

    在CustomControls项目bin里新建一个文件夹,把生成的Plugin.dll文件放入其中。然后运行就有一下效果了

    算是完成了。

    参考《.NET 控件开发基础》

  • 相关阅读:
    解决Ubuntu Kylin 1610安装ANSYS17.2的NVIDIA显卡驱动问题
    ubuntu安装ANSYS17.2全过程
    Ubuntu1604下安装Liggghts及CFDEM Coupling
    【Pyrosim案例】02:简单燃烧
    【Pyrosim案例】01:空气流动
    【FLUENT案例】06:与EDEM耦合计算
    【FLUENT案例】05:DDPM模型
    【FLUENT案例】04:利用DDPM+DEM模拟鼓泡流化床
    DataTables学习:从最基本的入门静态页面,使用ajax调用Json本地数据源实现前端开发深入学习,根据后台数据接口替换掉本地的json本地数据,以及报错的处理地方,8个例子(显示行附加信息,回调使用api,动态显示和隐藏列...),详细教程
    Python的下载和安装
  • 原文地址:https://www.cnblogs.com/xiaohuasan/p/5667536.html
Copyright © 2020-2023  润新知