Practical basic skills (14) -------- plug-in development of WPF

Source: Internet
Author: User
I. Solution Manager

Ii. Simple Functions

Imsg defines an interface. myplugin1 implements the interface function. "plug-in-type development" calls non-referenced DLL files when the program is running, and calls the DLL method implementation function.

Iii. Code for imsg, myplugin1 (class1 and class2), and plug-in development
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace imsg {// <summary> // This is the interface that the plug-in must implement and the only interface for communication between the main program and the plug-in. // In other words, the main program only recognizes these methods in the plug-in. // </Summary> Public interface imsgplug {void onshowdlg (); string onshowinfo ();}}
Using system; using system. collections. generic; using system. text; using imsg; namespace myplugin1 {public class myconsole: imsgplug {# region imsgplug member public void onshowdlg () {console. writeline ("onshowdlg method for calling INS in the console");} Public String onshowinfo () {return "myconsole" ;}# endregion }}
Using system; using system. collections. generic; using system. text; using system. windows. forms; using imsg; namespace myplugin1 {public class mydlg: form, imsgplug {# region imsgplug member public void onshowdlg () {This. TEXT = "ins subforms"; this. showdialog (); // call the showdialog of form to display the form} Public String onshowinfo () {return "mydlg" ;}# endregion }}
Main and important code of the program
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. windows; using system. windows. controls; using system. windows. data; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. imaging; using system. windows. navigation; using system. windows. shapes; using system. collections; using system. io; using system. reflecti On; namespace plug-in-type development {// <summary> // mainwindow. interaction logic of XAML // </Summary> Public partial class mainwindow: window {// set of plug-ins for private arraylist plugins = new arraylist (); Public mainwindow () {initializecomponent () ;}/// <summary> /// load all plug-ins /// </Summary> private void loadallplugs () {// obtain the plug-in directory (plugins) all files under // string [] files = directory. getfiles (application. resourceassembly + @ "\ plugins"); // Str Ing str6 = appdomain. currentdomain. setupinformation. applicationbase; // get the debug folder // str6 = system. diagnostics. process. getcurrentprocess (). mainmodule. filename; // obtain the EXE file // str6 = system. environment. currentdirectory; // get the debug folder // str6 = system. io. directory. getcurrentdirectory (); // get the debug folder // str6 = system. appdomain. currentdomain. basedirectory; // get the debug folder // str6 = system. appdomain. currentdo Main. setupinformation. applicationbase; // get the debug folder // str6 = system. windows. forms. application. startuppath; // get the debug folder // string [] files = directory. getfiles (appdomain. currentdomain + "plugins"); string [] files = directory. getfiles (@ "E: \ exercise code \ imsg \ plug-in-type development \ plugins"); // obtain the path of the DLL file foreach (string file in files) {If (file. toupper (). endswith (". DLL ") // if the file ends with DLL {try {// load DLL Assembly AB = assembly. Loadfrom (File); // type [] types = AB. gettypes (); // type array foreach (type T in types) {// if some classes implement predefined imsg. the imsgplug interface is considered to be compatible with the main program (the main program plug-in) if (T. getinterface ("imsgplug ")! = NULL) {plugins. add (AB. createinstance (T. fullname); // Add the instantiated DLL to the lstways in the generic set. items. add (T. fullname); // displayed in ListBox} catch (exception ex) {MessageBox. show (ex. message) ;}}}// form loading event private void window_loaded (Object sender, routedeventargs e) {loadallplugs ();} // call the plug-in method private void btngetin_click (Object sender, routedeventargs e) {If (this. lstways. selectedindex =-1) return; object selobj = This. plugins [this. lstways. selectedindex]; type T = selobj. getType (); methodinfo onshowdlg = T. getmethod ("onshowdlg"); // obtain the method methodinfo onshowinfo = T in the DLL file. getmethod ("onshowinfo"); // obtain the onshowdlg method in the DLL file. invoke (selobj, null); // method call parameter object returnvalue = onshowinfo. invoke (selobj, null );}}}
Iv. Postscript

When talking about plug-ins, we first think of Firefox. Anyone who has used Firefox knows that it is a plug-in program. When a function is required, you can download a plug-in from the Internet and restart it. This feature brings us a lot of convenience, which is the benefit of plug-in programs.
The essence of the plug-in is to expand and strengthen the software functions without modifying the program entity (Platform). When the plug-in interface is published, any company or individual can create their own plug-ins to solve some operational inconvenience or add new features, that is, to truly implement "plug-and-play" software development.
The Platform + plug-in software structure divides a target software to be developed into two parts: the main body or framework of the software, which can be defined as a platform. This is a pre-compiled program. The other part is a function or supplementary module, which can be defined as a plug-in. This is the plug-in program to be installed later.
Assume that your program has been deployed on your computer and runs properly. But one day, the user calls-they need to add new features. After determining the user's requirements, you can find that the original software architecture is no longer qualified for new tasks-you need to redesign this application! But the problem is that even if you use another development cycle to complete the applications required by the user, there is no guarantee that the user's requirements will not change again. That is to say, the possibility of spreading demand still exists. Therefore, the plug-in architecture can better demonstrate its superiority in this case.
It can be said that it can bring convenience, and it is also very easy to develop. In addition, such a main program does not need to be modified at all. You can use the plug-in when you need it. When the plug-in is updated, you only need to update the plug-in.
From the perspective of program development, it is generally to first develop the main program, decide which functions are completed by the main program, and then create an interface to declare the interface content, these contents determine the extension and direction of the plug-in functions. These are pre-prepared by the main program developers. The plug-in developer obtains the interface content from the main program developer, and writes classes that inherit these interfaces to complete specific functions.

Practical basic skills (14) -------- plug-in development of WPF

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.