Development example of plug-in framework ExpressPlugin

Source: Internet
Author: User

The following is a development process based on ExpressPlugin through an example program. This example program is a text editing tool similar to Windows notepad and provides the functions of creating, editing, and saving text files, the name is SimpleEditor.

1. Create a New WindowsApplication project in Visual Studio and name it SimpleEditor. 2. Add reference ExpressPlugin. dll for the new project. Select Browse from the Add reference window in VS environment, find the bin folder under the installation directory, and add the dll file in this folder to the project reference. 3. Create a text editing window.In this example, the RichTextBox Control provided by Windows is used as the editing control, which provides the text input and display functions. In the plug-in framework, any work window belongs to the view, and the IViewContent interface must be implemented to be recognized by the framework. Therefore, the IViewContent class must also be implemented when editing the window. The class is defined as follows:

 /// <Summary>
/// Text editing window
/// </Summary>
Class TextViewContent: AbstractViewContent
{
// Text editing Control
Private RichTextBox richTextBox = new RichTextBox ();

/// <Summary>
/// View control attributes (IViewContent)
/// </Summary>
Public override Control ViewContentControl
{
Get {return richTextBox ;}
}
/// <Summary>
/// Default constructor (create a new text file)
/// </Summary>
Public TextViewContent ()
: Base ()
{
}
/// <Summary>
/// Open an existing file
/// </Summary>
/// <Param name = "fileName"> file path </param>
Public TextViewContent (string fileName)
: Base (fileName)
{
This. FileName = fileName;
This. Title = Path. GetFileName (fileName );
If (File. Exists (fileName ))
{
StreamReader sr = new StreamReader (fileName, Encoding. Default );
String fileContent = sr. ReadToEnd ();
Sr. Close ();
This. richTextBox. Text = fileContent;
}
}
}
To save and save files in the future, you need to add two processing functions in this class:

Code

 /// <Summary>
/// Save the file
/// </Summary>
Public void SaveFile ()
{
// Opened files
If (File. Exists (this. FileName ))
{
This. richTextBox. SaveFile (this. FileName, RichTextBoxStreamType. PlainText );
}
Else
{
// Create a file
SaveFileDialog saveDialog = new SaveFileDialog ();
SaveDialog. AddExtension = true;
SaveDialog. Filter = "text file (*. txt, *. log, *. cs) | *. txt; *. log; *. cs ";
DialogResult dr = saveDialog. ShowDialog ();
If (dr = DialogResult. OK)
{
String fileName = saveDialog. FileName;
This. FileName = fileName;
This. Title = Path. GetFileName (fileName );
This. richTextBox. SaveFile (fileName, RichTextBoxStreamType. PlainText );
This. OnSaved (new SaveEventArgs (true ));
}
}
}

/// <Summary>
/// Save another file
/// </Summary>
Public void SaveAs ()
{
SaveFileDialog saveDialog = new SaveFileDialog ();
SaveDialog. AddExtension = true;
SaveDialog. Filter = "text file (*. txt, *. log, *. cs) | *. txt; *. log; *. cs ";
DialogResult dr = saveDialog. ShowDialog ();
If (dr = DialogResult. OK)
{
String fileName = saveDialog. FileName;
This. FileName = fileName;
This. Title = Path. GetFileName (fileName );
This. richTextBox. SaveFile (fileName, RichTextBoxStreamType. PlainText );
}
}
4. Implement functions.With the text editing window, you can now implement the specific text operation function. Common text operations include new, open, save, and save functions. Each function implementation corresponds to a class and implements the IMenuCommand interface. The IMenuCommand interface defines the menu state control and call entry. The sample code is as follows:

Code

 [CommandUsage ("new file")]
Class doNewFile: AbstractMenuCommand
{
Public override void Run ()
{
// Create a text editing View
TextViewContent view = new TextViewContent ();
// Display view
WorkbenchSingleton. Workbench. ShowView (view );
}
}

[CommandUsage ("saving files")]
Class doSaveFile: AbstractMenuCommand
{
// Control the available menu status
Public override bool IsEnabled
{
Get
{
// The Save button can be activated only when the current text editing window is not empty.
Return WorkbenchSingleton. Workbench. ActiveViewContent! = Null;
}
}

Public override void Run ()
{
IViewContent activeView = WorkbenchSingleton. Workbench. ActiveViewContent;
If (activeView! = Null & activeView is TextViewContent)
{
TextViewContent textView = activeView as TextViewContent;
// The save path selection box is displayed and the text content is saved.
TextView. SaveFile ();
}
}
}
5. Configure the system menu and interface.After writing the function code, you need to specify the call entry menu for each function. The menu also shows the application. Therefore, you need to use tools to configure menus and application interfaces. The configuration information includes the following categories (corresponding to the plug-in type): A: Application name and other description information. B: run the database. The Runtime Library is the result of compilation of the plug-in Function Code (dll or exe). The settings here must ensure that the file name path is correct, otherwise the application cannot run. C: startup Item. Set the functions automatically executed after the system starts. This type of initialization operations, such as logging on or loading resources, are usually performed. D: System Panel. The Panel is the main component of the application interface. It must be set to the implementation class corresponding to the Panel. The name, icon, and shortcut keys are optional. E: Main Menu, right-click menu, and toolbar. The name of the main menu and right-click menu is optional, and the toolbar button must be set as an icon; otherwise, it cannot be displayed. 6. added the startup interface.If the startup interface is enabled, save the image as splashscreen.jpg?splashscreen.gif and place it in the startup directory. 7. Start the application.After the above steps, the application prototype has come out, start up and take a look, and then perform further development and improvement.

Download: ExpressPlugin_SetupV2.0.rar

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.