随笔 - 44  文章 - 0 评论 - 302 trackbacks - 80
<2007年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

本博客上的所有文章如非特别说明均为原创,如果要转载请注明文章出处。

与我联系

搜索

 

常用链接

留言簿(19)

我参加的小组

我参与的团队

随笔分类(40)

随笔档案(44)

文章分类

联系我

友情链接

积分与排名

  • 积分 - 169532
  • 排名 - 205

最新评论

阅读排行榜

评论排行榜

       我在前一篇文章里提到,对于停靠工具栏或者是视图最好是不要将实例放到词典中,而是将工具栏或者视图的类型放到词典中,因为视图类型会经常的被重用,并且会经常被关闭或者再打开。当实例被关闭后,资源就被释放了,对于实例的管理就会比较麻烦,所以我们分为两步走。在插件被加载的时候,我们只注册类型,在应用程序运行的时候,我们通过某种途径来实例化他。
       我修改的以前的例子,主要突出本次演示的功能。这次的例子实现的功能是通过插件扩展应用程序处理不同文件的能力。在原始的应用程序中,我们可以通过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<stringstring>();
        
private Dictionary<String, String> fileFilterToFileType = new Dictionary<stringstring>();
        
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,为两种文件注册了这个视图类型,大家可以根据自己的需要继续扩展。转眼又十一点多了,明天还要上班,就写到这里了,又说的不清楚的地方,大家可以参考一下源代码。
   
源代码


FeedBack:
#1楼  2007-05-15 07:34 David.Dong [未注册用户]
太好啦,谢谢老大!
  回复  引用    
#2楼  2007-05-15 09:07 3echo      
一直关注,学习!
  回复  引用  查看    
#3楼  2007-05-15 10:10 一滴水      
关注中,最近在学习SharpDevelop的插件系统,看了你的文章很有启发,谢谢了
  回复  引用  查看    
#4楼  2007-05-15 17:01 Leepy      
勾上Plugin以后,在勾去Plugin,为什么还是有显示.jpg和.bmp文件选择?
  回复  引用  查看    
#5楼 [楼主] 2007-05-15 23:41 纶巾客      
@Leepy
呵呵,时间太紧,只做了个简单的演示。我只在plugin的load方法里注册了类型,但是在Unload方法里,没有写删除注册的内容。
  回复  引用  查看    
#6楼  2007-05-16 19:26 icom99 [未注册用户]
Great...
  回复  引用    
#7楼  2007-07-11 20:57 oxsoft.cn [未注册用户]
没有了吗?
  回复  引用    
不错的思路,好
  回复  引用  查看    
不在写了吗?
  回复  引用    
楼主你好,看了你的这个系列很受启发。其中你谈到各个Service容器构成了一个Service容器树,每一个节点的服务都可以一直向上传递,直到根部,而每一个节点请求Service的时候,我们总是可以从根节点获得。

我新建了个插件,它有自己的服务,我在Load()函数里
也有加如服务
application.AddService(typeof(IPlugin2Server), p2s,true);

但我在主框架里通过serviceContainer.GetService却返回的NULL,我通过跟踪发现服务确实已经加入进去,但不知道为什么就是取不出来呢?


  回复  引用    
#11楼  2007-12-27 16:38 哈哈1 [未注册用户]
基本上算是有点明白了,希望楼主继续!
  回复  引用    
写得很好,坚持
对此无私的行为表示高度赞赏...
  回复  引用    

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-03-05 17:36 编辑过


相关链接: