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

本文介绍了一种通过插件扩展应用程序处理多种文件格式的方法。使用IDocumentViewService接口和服务的实现,允许应用程序动态地注册和支持不同的文件类型,如文本和图片文件。这包括展示如何在运行时实例化视图和更新文件过滤选项。
我在前一篇文章里提到,对于停靠工具栏或者是视图最好是不要将实例放到词典中,而是将工具栏或者视图的类型放到词典中,因为视图类型会经常的被重用,并且会经常被关闭或者再打开。当实例被关闭后,资源就被释放了,对于实例的管理就会比较麻烦,所以我们分为两步走。在插件被加载的时候,我们只注册类型,在应用程序运行的时候,我们通过某种途径来实例化他。
       我修改的以前的例子,主要突出本次演示的功能。这次的例子实现的功能是通过插件扩展应用程序处理不同文件的能力。在原始的应用程序中,我们可以通过File菜单的Open,只能打开一种文件,就是文本文件,大家可以在例子中看到,当我们没有加载插件的情况下,在OpenFileDialog的Filter中只有“Text(*.txt)”。选择一个文本文件以后,将会出现文本文件视图。当我们加载插件以后,在点击File->Open菜单,我们观察Filter,发现会多出两种文件:“JPEG”和“BMP”,这是我们就可以打开图片文件,选中文件以后,将会出现Picture视图,并且在主菜单下边,还会出现一个工具栏,点击工具栏上的按钮,可以给图片加上水印,并且工具栏会根据PictureView的状态(Active)显示和消失。比如你打开了一个文本视图和一个图片视图,当你切换到文本视图的时候,工具栏就会消失,再切换到图片视图的时候,工具栏又会出现。
       我在框架里面添加了一个IDocumentViewService的接口,用以描述服务的功能:
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Collections.Specialized;
None.gif
None.gif
namespace PluginFramework
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public interface IDocumentViewService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
void RegisterView(String fileType,string fileFilter,Type viewType);
InBlock.gif        
void ShowView(String fileType, String filePath);
InBlock.gif        
void RemoveRegister(String fileType);
InBlock.gif        String GetFileFilter(String fileType);
InBlock.gif        String GetFileTypeByFileFilter(String fileFilter);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        StringCollection FileTypies 
dot.gifget;}
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

     下面是这个服务的实现:
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Collections.Specialized;
None.gif
None.gif
namespace PluginFramework
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class DocumentViewService:IDocumentViewService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private Dictionary<String, Type> docViewRegister = new Dictionary<string, Type>();
InBlock.gif        
private Dictionary<String, String> fileTypeToFileFilter = new Dictionary<stringstring>();
InBlock.gif        
private Dictionary<String, String> fileFilterToFileType = new Dictionary<stringstring>();
InBlock.gif        
private IApplication application = null;
InBlock.gif
InBlock.gif        
public DocumentViewService(IApplication app)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            application 
= app;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IDocumentViewService Members#region IDocumentViewService Members
InBlock.gif
InBlock.gif        
public void RegisterView(string fileType, string fileFilter, Type viewType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            docViewRegister[fileType] 
= viewType;
InBlock.gif            fileTypeToFileFilter[fileType] 
= fileFilter.ToUpper();
InBlock.gif            fileFilterToFileType[fileFilter.ToUpper()] 
= fileType;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void ShowView(string fileType, string filePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(docViewRegister.ContainsKey(fileType))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                IDocumentView docView 
= null;
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    docView 
= (IDocumentView)Activator.CreateInstance(docViewRegister[fileType]);
InBlock.gif                    docView.Application 
= application;
InBlock.gif                    docView.ShowView(filePath);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void RemoveRegister(string fileType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            docViewRegister.Remove(fileType);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public StringCollection FileTypies
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                StringCollection sc 
= new StringCollection();
InBlock.gif                
foreach (String key in docViewRegister.Keys)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sc.Add(key);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return sc;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IDocumentViewService Members#region IDocumentViewService Members
InBlock.gif
InBlock.gif
InBlock.gif        
public string GetFileFilter(string fileType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            String result 
= "";
InBlock.gif            
if (fileTypeToFileFilter.ContainsKey(fileType))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result 
= fileTypeToFileFilter[fileType];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IDocumentViewService Members#region IDocumentViewService Members
InBlock.gif
InBlock.gif
InBlock.gif        
public string GetFileTypeByFileFilter(string fileFilter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            String result 
= "";
InBlock.gif            
if (fileFilterToFileType.ContainsKey(fileFilter))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result 
= fileFilterToFileType[fileFilter];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值