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

Source: Internet
Author: User

In the previous articleArticleWe mentioned that it is best not to put instances in the dictionary for the tool bar or view to be docked, but to put the tool bar or view type in the dictionary, because the view type is often reused, it is often disabled 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.ProgramDuring running, we can 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 tight and the writing is rough. documentview is the path of the file to be opened and the display method. After the plug-in, I implemented a pictureview , you have registered this view type for two types of files. You can continue to expand as needed. In the twinkling of an eye, it's more than 11 o'clock. I will go to work tomorrow and write it here. What I'm talking about is unclear, for more information, see Source Code .
source Code

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.