现在的位置: 首页 > 综合 > 正文

构建插件式的应用程序框架(七)-基本服务

2011年11月24日 ⁄ 综合 ⁄ 共 3102字 ⁄ 字号 评论关闭
既然做好了框架,我们就希望为某个目标服务,我们要提供一些基本的服务,方便用户继续扩展他的功能。首先想到的功能就是,菜单,工具栏的管理,接下来我们要实现一些更流行的功能,比如停靠工具栏等等。

如何实现这些服务呢?我们希望我们的插件在运行时可以获得应用程序本身的菜单,工具条,停靠工具栏等等,然后向他们添加项目,比如加入一个菜单项,添加一个工具栏按钮。为了在运行时获得某个菜单或者工具栏,我们要为每一个菜单后者工具栏分配一个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+滚轮缩放图片

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

源代码
http://files.cnblogs.com/guanjinke/pluginsample3.rar

抱歉!评论已关闭.