zoukankan      html  css  js  c++  java
  • 构建插件式的应用程序框架(七)-基本服务

    既然做好了框架,我们就希望为某个目标服务,我们要提供一些基本的服务,方便用户继续扩展他的功能。首先想到的功能就是,菜单,工具栏的管理,接下来我们要实现一些更流行的功能,比如停靠工具栏等等。

    如何实现这些服务呢?我们希望我们的插件在运行时可以获得应用程序本身的菜单,工具条,停靠工具栏等等,然后向他们添加项目,比如加入一个菜单项,添加一个工具栏按钮。为了在运行时获得某个菜单或者工具栏,我们要为每一个菜单后者工具栏分配一个Key,然后放到一个词典中,当需要的时候,我们通过这个key来获得实例。对于这个Key呢,在我的例子比较简单就是他的名字,我们来看看ToolStripService的代码:
    复制  保存
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    namespace PluginFramework
    {
    public class ToolStripService : IToolStripService
    {
    private IApplication application = null;
    private Dictionary<String, ToolStrip> toolStrips = new Dictionary<string, ToolStrip>();
    public ToolStripService(IApplication application)
    {
    this.application = application;
    }
    
    #region IToolStripService Members
    
    public System.Windows.Forms.ToolStrip GetToolStrip(string toolStripName)
    {
    ToolStrip toolStrip = null;
    if (toolStrips.ContainsKey(toolStripName))
    {
    toolStrip = toolStrips[toolStripName];
    }
    return toolStrip;
    }
    public void AddToolStrip(string toolStripName, System.Windows.Forms.ToolStrip toolStrip)
    {
    if (toolStrips.ContainsKey(toolStripName))
    {
    MessageBox.Show("The tool strip name has existed!");
    }
    else
    {
    toolStrips[toolStripName] = toolStrip;
    //如果没有指定toolstrip在哪个面板,择默认加到顶部
                    if (application.TopToolPanel != null)
    {
    application.TopToolPanel.Controls.Add(toolStrip);
    }
    }
    }
    public void AddToolStrip(string toolStripName,
    System.Windows.Forms.ToolStrip toolStrip,
    ToolStripDockState option)
    {
    if (toolStrips.ContainsKey(toolStripName))
    {
    MessageBox.Show("The tool strip name has existed!");
    }
    else
    {
    toolStrips[toolStripName] = toolStrip;
    switch (option)
    {
    case ToolStripDockState.Left:
    if (application.LeftToolPanel != null)
    {
    application.LeftToolPanel.Controls.Add(toolStrip);
    }
    break;
    case ToolStripDockState.Right:
    if (application.RightToolPanel != null)
    {
    application.RightToolPanel.Controls.Add(toolStrip);
    }
    break;
    case ToolStripDockState.Top:
    if (application.TopToolPanel != null)
    {
    application.TopToolPanel.Controls.Add(toolStrip);
    }
    break;
    case ToolStripDockState.Bottom:
    if (application.BottomToolPanel != null)
    {
    application.BottomToolPanel.Controls.Add(toolStrip);
    }
    break;
    }
    }
    }
    public void RemoveToolStrip(string toolStripName)
    {
    ToolStrip toolStrip = GetToolStrip(toolStripName);
    if (toolStrip != null)
    {
    if (application.TopToolPanel != null
    && application.TopToolPanel.Controls.Contains(toolStrip))
    {
    application.TopToolPanel.Controls.Remove(toolStrip);
    }
    else if (application.BottomToolPanel != null
    && application.BottomToolPanel.Controls.Contains(toolStrip))
    {
    application.BottomToolPanel.Controls.Remove(toolStrip);
    }
    else if (application.LeftToolPanel != null
    && application.LeftToolPanel.Controls.Contains(toolStrip))
    {
    application.LeftToolPanel.Controls.Remove(toolStrip);
    }
    else if (application.RightToolPanel != null
    && application.RightToolPanel.Controls.Contains(toolStrip))
    {
    application.RightToolPanel.Controls.Remove(toolStrip);
    }
    }
    toolStrips.Remove(toolStripName);
    }
    
    #endregion
        }
    }

    对于视图或者是停靠工具栏来说,最好是不要直接在词典中放入实例,而是应该将对象的类型放入到词典中,因为,视图和停靠工具栏本身都是从Form派生而来,所以,当视图或者是停靠工具栏被关闭的时候,对象就被销毁了,而对象的创建在是插件的Load方法里完成的,我们不可能再去调用插件的Load方法,这样给我们的使用带来了不便,所以我们应该注册类型,然后在Service中实现一个Show方法是比较合理的,这里为了演示方便,我就直接在Load里面实例化了,并把实例放到了词典里。

    下边这个图例里显示了插件加入的停靠工具栏,工具栏,一个新的菜单“View”和View菜单的子菜单:
    单击显示全图,Ctrl+滚轮缩放图片


    最近实在是没有时间,文章发的很慢,也写的很错,说的不清楚的地方,可以参考一下源代码,望各位朋友见谅。

    源代码
    https://files.cnblogs.com/guanjinke/pluginsample3.rar
  • 相关阅读:
    面试题15:链表中倒数第K个结点
    面试题31:连续子数组的最大和
    数据库索引实例
    面试题27:二叉搜索树与双向链表
    面试题28:字符串的排列
    java比较器Comparable接口和Comaprator接口
    面向对象知识汇总
    虚函数与纯虚函数
    Linux IO实时监控iostat命令详解
    hive GroupBy操作(翻译自Hive wiki)
  • 原文地址:https://www.cnblogs.com/lzhdim/p/1368762.html
Copyright © 2011-2022 走看看