构建插件式的应用程序框架(八)----视图服务的简单实现

简介:

   我在前一篇文章里提到,对于停靠工具栏或者是视图最好是不要将实例放到词典中,而是将工具栏或者视图的类型放到词典中,因为视图类型会经常的被重用,并且会经常被关闭或者再打开。当实例被关闭后,资源就被释放了,对于实例的管理就会比较麻烦,所以我们分为两步走。在插件被加载的时候,我们只注册类型,在应用程序运行的时候,我们通过某种途径来实例化他。
       我修改的以前的例子,主要突出本次演示的功能。这次的例子实现的功能是通过插件扩展应用程序处理不同文件的能力。在原始的应用程序中,我们可以通过File菜单的Open,只能打开一种文件,就是文本文件,大家可以在例子中看到,当我们没有加载插件的情况下,在OpenFileDialog的Filter中只有"Text(*.txt)"。选择一个文本文件以后,将会出现文本文件视图。当我们加载插件以后,在点击File->Open菜单,我们观察Filter,发现会多出两种文件:"JPEG"和"BMP",这是我们就可以打开图片文件,选中文件以后,将会出现Picture视图,并且在主菜单下边,还会出现一个工具栏,点击工具栏上的按钮,可以给图片加上水印,并且工具栏会根据PictureView的状态(Active)显示和消失。比如你打开了一个文本视图和一个图片视图,当你切换到文本视图的时候,工具栏就会消失,再切换到图片视图的时候,工具栏又会出现。
       我在框架里面添加了一个IDocumentViewService的接口,用以描述服务的功能:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;

namespace PluginFramework
{
    
public interface IDocumentViewService
    {
        
void RegisterView(String fileType,string fileFilter,Type viewType);
        
void ShowView(String fileType, String filePath);
        
void RemoveRegister(String fileType);
        String GetFileFilter(String fileType);
        String GetFileTypeByFileFilter(String fileFilter);

        StringCollection FileTypies { 
get;}
    }
}


     下面是这个服务的实现:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;

namespace PluginFramework
{
    
public class DocumentViewService:IDocumentViewService
    {
        
private Dictionary<String, Type> docViewRegister = new Dictionary<string, Type>();
        
private Dictionary<String, String> fileTypeToFileFilter = new Dictionary<string, string>();
        
private Dictionary<String, String> fileFilterToFileType = new Dictionary<string, string>();
        
private IApplication application = null;

        
public DocumentViewService(IApplication app)
        {
            application = app;
        }

        
IDocumentViewService Members#region IDocumentViewService Members

        
public void RegisterView(string fileType, string fileFilter, Type viewType)
        {
            docViewRegister[fileType] = viewType;
            fileTypeToFileFilter[fileType] = fileFilter.ToUpper();
            fileFilterToFileType[fileFilter.ToUpper()] = fileType;
        }

        
public void ShowView(string fileType, string filePath)
        {
            
if(docViewRegister.ContainsKey(fileType))
            {
                IDocumentView docView = 
null;
                
try
                {
                    docView = (IDocumentView)Activator.CreateInstance(docViewRegister[fileType]);
                    docView.Application = application;
                    docView.ShowView(filePath);
                }
                
catch
                {
                    
                }
                
            }
        }

        
public void RemoveRegister(string fileType)
        {
            docViewRegister.Remove(fileType);
        }

        
public StringCollection FileTypies
        {
            
get
            {
                StringCollection sc = 
new StringCollection();
                
foreach (String key in docViewRegister.Keys)
                {
                    sc.Add(key);
                }
                
return sc;
            }
        }

        
#endregion


        
IDocumentViewService Members#region IDocumentViewService Members


        
public string GetFileFilter(string fileType)
        {
            String result = "";
            
if (fileTypeToFileFilter.ContainsKey(fileType))
            {
                result = fileTypeToFileFilter[fileType];
            }
            
return result;
        }

        
#endregion

        
IDocumentViewService Members#region IDocumentViewService Members


        
public string GetFileTypeByFileFilter(string fileFilter)
        {
            String result = "";
            
if (fileFilterToFileType.ContainsKey(fileFilter))
            {
                result = fileFilterToFileType[fileFilter];
            }
            
return result;
        }

        
#endregion
    }
}

      时间比较紧,写的比较粗糙。另外定义了DocumentView的基本功能,就是需要打开的文件的路径,以及显示的方法。再插件了,我实现的一个PictureView,为两种文件注册了这个视图类型,大家可以根据自己的需要继续扩展。转眼又十一点多了,明天还要上班,就写到这里了,又说的不清楚的地方,大家可以参考一下源代码。






本文转自纶巾客博客园博客,原文链接:http://www.cnblogs.com/guanjinke/archive/2007/05/14/746481.html,如需转载请自行联系原作者
目录
相关文章
|
弹性计算 安全 关系型数据库
ECS使用心得体验
强烈的编程爱好让我在编程的道路上越走越远,也让我学到了非常多的知识,同时也发现,学得越多,自己不懂的就越多,只有不断的学习,才能让自己跟上时代发展的步伐。由于在学习过程中使用到了云服务器,然后通过学长学姐的推荐,我了解到了“飞天加速计划•高校学生在家实践”活动,也因此第一次使用了ECS,由此产生了一些使用心得。
|
22小时前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
7536 0
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17836 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
1天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
743 0
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1429 5
|
15天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2496 8