WinForm WPF基于一个脚本引擎(ReoScript)的窗体设计器开发总结(三)

0x02   界面设计器

       网上有很多界面设计器的代码,但实际上分为两种,一种自己写控件,画 designPanel ,画控件环闹 一种是使用微软自带的DesignSurface 我两种都试过,

第一种比较老土,坑也比较多,比如在给 PropertyGrid 设置SelectedObject 的时候,我希望能设置控件的name 属性,但是 SelectedObject  直接设为控件是不行的,

name 属性 是显示不出来的,只能通过反射了。 最后我在 通过反射与标准控件进行对比,来生成脚本代码,当时心中 有一万个草泥马 奔过。。。。

       下面主要用的是第二种,这种.netFW 2.0 框架就已经自带了,微软的技术肯定好一点。

核心在与给 DesignSurface 添加 各种各样的服务,从而达到丰富设计器功能的目的,部分service 接口可能要自己去实现。

比如这样:

 1   private void RegisterDesignService(DesignSurface designSurface) {
 2             // get host design surface
 3             IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
 4 
 5             if (host != null) {
 6                 CodeDomComponentSerializationService codeDomComponentSerializationService
 7                     = new CodeDomComponentSerializationService(host);
 8                 NameCreationService nameCreationService = new NameCreationService(host);
 9                 DesignerSerializationService designerSerializationService =
10                     new DesignerSerializationService(host);
11                 Services.EventBindingService eventBindingService =
12                     new Services.EventBindingService(host);
13                 MenuCommandService menuCommandService = new MenuCommandService(host);
14                 WindowsFormsEditorService editorService = new WindowsFormsEditorService();
15                 DesignerEventService designerEventService = new DesignerEventService();
16                 // add service to  this designSurface
17                 host.AddService(typeof(IToolboxService), toolBox);
18                 host.AddService(typeof(ComponentSerializationService), codeDomComponentSerializationService);
19                 host.AddService(typeof(INameCreationService), nameCreationService);
20                 host.AddService(typeof(IDesignerSerializationService), designerSerializationService);
21                 host.AddService(typeof(IEventBindingService), eventBindingService);
22                 host.AddService(typeof(IWindowsFormsEditorService), editorService);
23                 host.AddService(typeof(IMenuCommandService), menuCommandService);
24                 host.AddService(typeof(IDesignerEventService), designerEventService);
25                 //DesignerActionUIService
26                 //DesignerActionService
27                 //IDesignerEventService
28                 //
29             }
30             // buliding select component ecnets
31             ISelectionService selectionService = host.GetService(typeof(ISelectionService)) as ISelectionService;
32             selectionService.SelectionChanged += SelectionService_SelectionChanged;
33 
34             IComponentChangeService componentChangeService =
35                 host.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
36             componentChangeService.ComponentRemoving += ComponentChangeService_ComponentRemoving;
37 
38             // sactive 
39             designSurfaceManager.ActiveDesignSurface = designSurface;
40 
41         }
View Code

下面我直接给出微软的demo,这部分就不在更新了,因为这个设计器我自己还在改,这种东西只能作为学习的demo 实际上没什么用,

作为一个.net 开发者,我们应该更多的去学习现在比较流行的 .net core,Xamarin, 或者比较火的前端,学这些 东西更有用一点。如果你想学习设计器这方面的技术

可以看看SharpDevelop 在github 的代码(代码看起来很标准,但英文比较烂的我一阵头大, 也可能是架构太先进的缘故....)

附件:

 DesignerHosting.zip

猜你喜欢

转载自www.cnblogs.com/Lite/p/9024710.html
今日推荐