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

简介: 既然做好了框架,我们就希望为某个目标服务,我们要提供一些基本的服务,方便用户继续扩展他的功能。首先想到的功能就是,菜单,工具栏的管理,接下来我们要实现一些更流行的功能,比如停靠工具栏等等。 如何实现这些服务呢?我们希望我们的插件在运行时可以获得应用程序本身的菜单,工具条,停靠工具栏等等,然后向他们添加项目,比如加入一个菜单项,添加一个工具栏按钮。
既然做好了框架,我们就希望为某个目标服务,我们要提供一些基本的服务,方便用户继续扩展他的功能。首先想到的功能就是,菜单,工具栏的管理,接下来我们要实现一些更流行的功能,比如停靠工具栏等等。

如何实现这些服务呢?我们希望我们的插件在运行时可以获得应用程序本身的菜单,工具条,停靠工具栏等等,然后向他们添加项目,比如加入一个菜单项,添加一个工具栏按钮。为了在运行时获得某个菜单或者工具栏,我们要为每一个菜单后者工具栏分配一个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
目录
相关文章
|
4天前
|
存储 人工智能 安全
AI 越智能,数据越危险?
阿里云提供AI全栈安全能力,为客户构建全链路数据保护体系,让企业敢用、能用、放心用
|
7天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
6天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
407 93
|
6天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
401 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
|
6天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
294 158
|
14天前
|
机器人 API 调度
基于 DMS Dify+Notebook+Airflow 实现 Agent 的一站式开发
本文提出“DMS Dify + Notebook + Airflow”三位一体架构,解决 Dify 在代码执行与定时调度上的局限。通过 Notebook 扩展 Python 环境,Airflow实现任务调度,构建可扩展、可运维的企业级智能 Agent 系统,提升大模型应用的工程化能力。