WinForm中实现插件式开发
插件式开发的好处很多,这里只说说借助ICSharpCode来实现插件式开发的过程。
首先下载ICSharpCode.Core.dll和ICSharpCode.Core.WinForms.dll,然后配置一个Addin文件,取名为SubMenu.Addin,内容如下:
1<AddIn name = "ShtxSmsSum"
2 author = "JasonChou"
3 url = "http://www.xxxx.cn"
4 description = "外部插件">
5
6 <Manifest>
7
8 </Manifest>
9
10 <!--Runtime节指定了插件功能模块所在的库文件MainForm.dll的具体路径-->
11 <Runtime>
12 <Import assembly="ShtxSmsSum.PlugIn.dll"/>
13 </Runtime>
14
15 <!--Path节中指定了扩展点路径/JasonChou/myMenus(该路径是指在程序运行时自动运行该插件),然后在Path内指定了它的ID、Command类名-->
16 <Path name = "/JasonChou/myMenus">
17 <MenuItem id = "External_Menu"
18 type = "Menu"
19 label = "其它工具"
20 insertbefore = "Manager">
21 <MenuItem id ="SmsAccountTool"
22 label="短信统计工具"
23 class="ShtxSmsSum.PlugIn.SmsAccountTool"
24 />
25 <MenuItem id ="SearchHistoryCommand"
26 label="历史价格查询"
27 class="ShtxSmsSum.PlugIn.SearchHistoryCommand"
28 />
29 <MenuItem id ="SearchSendSumCommand"
30 label="查询发送数量"
31 class="ShtxSmsSum.PlugIn.SearchSendSumCommand"
32 />
33 </MenuItem>
34 </Path>
35
36
37</AddIn>
38
2 author = "JasonChou"
3 url = "http://www.xxxx.cn"
4 description = "外部插件">
5
6 <Manifest>
7
8 </Manifest>
9
10 <!--Runtime节指定了插件功能模块所在的库文件MainForm.dll的具体路径-->
11 <Runtime>
12 <Import assembly="ShtxSmsSum.PlugIn.dll"/>
13 </Runtime>
14
15 <!--Path节中指定了扩展点路径/JasonChou/myMenus(该路径是指在程序运行时自动运行该插件),然后在Path内指定了它的ID、Command类名-->
16 <Path name = "/JasonChou/myMenus">
17 <MenuItem id = "External_Menu"
18 type = "Menu"
19 label = "其它工具"
20 insertbefore = "Manager">
21 <MenuItem id ="SmsAccountTool"
22 label="短信统计工具"
23 class="ShtxSmsSum.PlugIn.SmsAccountTool"
24 />
25 <MenuItem id ="SearchHistoryCommand"
26 label="历史价格查询"
27 class="ShtxSmsSum.PlugIn.SearchHistoryCommand"
28 />
29 <MenuItem id ="SearchSendSumCommand"
30 label="查询发送数量"
31 class="ShtxSmsSum.PlugIn.SearchSendSumCommand"
32 />
33 </MenuItem>
34 </Path>
35
36
37</AddIn>
38
如果以上代码有不明白的,先不管它,继续看下面之后会明白的,在主程序的Program.cs文件main方法中加入如下代码:
1Assembly asm = Assembly.GetExecutingAssembly();
2FileUtility.ApplicationRootPath = Path.GetDirectoryName(asm.Location);
3CoreStartup coreStartup = new CoreStartup("ShtxSmsSum");
4coreStartup.PropertiesName = "ShtxSmsSum";
5coreStartup.StartCoreServices();
6// 在指定文件夹中搜寻插件的配置文件
7coreStartup.AddAddInsFromDirectory(Path.Combine(FileUtility.ApplicationRootPath, "PlusIn"));
8// AddinManager 插件的属性:保存用户禁用的插件信息
9coreStartup.ConfigureExternalAddIns(Path.Combine(PropertyService.ConfigDirectory, "AddIns.xml"));
10// AddinManager 插件的属性:保存用户安装的插件信息
11coreStartup.ConfigureUserAddIns(Path.Combine(PropertyService.ConfigDirectory, "AddIns"),
12Path.Combine(PropertyService.ConfigDirectory, "AddIns"));
13coreStartup.RunInitialization();
2FileUtility.ApplicationRootPath = Path.GetDirectoryName(asm.Location);
3CoreStartup coreStartup = new CoreStartup("ShtxSmsSum");
4coreStartup.PropertiesName = "ShtxSmsSum";
5coreStartup.StartCoreServices();
6// 在指定文件夹中搜寻插件的配置文件
7coreStartup.AddAddInsFromDirectory(Path.Combine(FileUtility.ApplicationRootPath, "PlusIn"));
8// AddinManager 插件的属性:保存用户禁用的插件信息
9coreStartup.ConfigureExternalAddIns(Path.Combine(PropertyService.ConfigDirectory, "AddIns.xml"));
10// AddinManager 插件的属性:保存用户安装的插件信息
11coreStartup.ConfigureUserAddIns(Path.Combine(PropertyService.ConfigDirectory, "AddIns"),
12Path.Combine(PropertyService.ConfigDirectory, "AddIns"));
13coreStartup.RunInitialization();
这段的作用是在程序开始运行时就加载Addin文件里面的内容。然后在主窗体的构造方法中加入下面一句:
MenuService.AddItemsToMenu(menuStrip1.Items, this, "/JasonChou/myMenus");
这样就把Addin里面配置的菜单加入了主窗体的menuStrip1这个菜单控件中。
下面就是开发插件了,新建一个类库项目,这里取名叫ShtxSmsSum.PlugIn,下面有三个窗体Form1、SearchHistory、SearchSendSum,把相应功能做好,进入最后一步:
在此ShtxSmsSum.PlugIn项目中新建一个类,我取名叫CommandMenu.cs,里面代码如下:
1namespace ShtxSmsSum.PlugIn
2{
3 public class SmsAccountTool : AbstractMenuCommand
4 {
5 public override void Run()
6 {
7 Form1 st = new Form1();
8 st.Show();
9 }
10 }
11
12 public class SearchHistoryCommand : AbstractMenuCommand
13 {
14 public override void Run()
15 {
16 SearchHistory sh = SearchHistory.GetInstance();
17 sh.Show();
18 }
19 }
20
21 public class SearchSendSumCommand : AbstractMenuCommand
22 {
23 public override void Run()
24 {
25 SearchSendSum ss = SearchSendSum.GetInstance();
26 ss.Show();
27 }
28 }
29}
2{
3 public class SmsAccountTool : AbstractMenuCommand
4 {
5 public override void Run()
6 {
7 Form1 st = new Form1();
8 st.Show();
9 }
10 }
11
12 public class SearchHistoryCommand : AbstractMenuCommand
13 {
14 public override void Run()
15 {
16 SearchHistory sh = SearchHistory.GetInstance();
17 sh.Show();
18 }
19 }
20
21 public class SearchSendSumCommand : AbstractMenuCommand
22 {
23 public override void Run()
24 {
25 SearchSendSum ss = SearchSendSum.GetInstance();
26 ss.Show();
27 }
28 }
29}
继承ICSharpCode.Core中的AbstractMenuCommand类,然后重写Run方法,在Run方法中把新窗体显示出来即可。这样就完成了一个很简单的插件式开发,运行结果如下: