Build a plug-in Application Framework (8)-simple implementation of view services

Source: Internet
Author: User

As I mentioned in the previous article, it is best not to put instances in the dictionary for the tool bar or view to dock, but to put the tool bar or view type in the dictionary, this is because the view type is often reused and often closed or re-opened. When the instance is closed, the resource is released, and instance management is troublesome. Therefore, two steps are taken. When the plug-in is loaded, we only register the type. When the application is running, we instantiate it in some way.
The previous example I modified highlights the features of this demo. In this example, the function is to extend the ability of applications to process different files through plug-ins. In the original application, we can Open only one File through the Open File menu, which is a text File. As you can see in the example, when the plug-in is not loaded, only "Text (*. txt )". After you select a text file, the text file view is displayed. After loading the plug-in, click the File> Open menu and observe the Filter. Two more files are displayed: "JPEG" and "BMP ", here we can open the image file. After selecting the file, the Picture view will appear, and a toolbar will appear under the main menu. click the button on the toolbar, you can add a watermark to the image, and the toolbar will display and disappear according to the status (Active) of PictureView. For example, if you open a text view and an image view, when you switch to the text view, the toolbar disappears and then switches to the image view.
I added an IDocumentViewService interface in the framework to describe the functions of the Service:

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 ;}
}
}


The following is the implementation of this service:

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
}
}

The time is relatively tight and the writing is rough. In addition, the basic function of DocumentView is defined, that is, the path of the file to be opened and the display method. After the plug-in, I implemented a PictureView and registered this view type for the two files. You can continue to expand it as needed. In the twinkling of an eye, there will be more than 11 o'clock. I will go to work tomorrow and write it here. I am not clear about it. You can refer to the source code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.