.NET创建宿主设计器--DesignHost、DesignSurface.

  一个窗口在运行时,是这样的:

  
  但是,在设计时,却远比这复杂的多,它需要一个设计器对象:它仅存在于设计时,并连接到运行时存在的对象。

  

   宿主容器

  我们可以看到每个窗体和按钮均有与之相关的设计器。这两个对象也连接到拥有这两个对象的宿主容器。宿主容器还提供以下服务:选择服务(选择界面上的控件)、显示消息的UI服务、调用帮助、与开发环境交互等。

 

  另外,宿主容器还承担许多职责。它创建组件,将它们绑定到设计器,并为其维护的组件和设计器提供服务。它从某种持久性状态加载设计器,并将它们保存回该状态。宿主容器提供撤销逻辑、剪贴板功能、以及其他服务 — 设计器需要以其为基础来提供一个健壮的设计时环境。

 

  利用服务提高可扩展性

  .NET Framework 设计器体系结构是可扩展的。可扩展性的关键在于,服务能够增强各种设计器的可用功能。

  服务是一种对象,可根据类型进行查询。通常,您定义一些代表服务的抽象类或接口,然后提供对该服务的实现。您可以将服务添加到调用服务容器的对象,也可以从该对象中删除服务。

  IDesignerHost — 设计器的主要宿主接口,它是一个服务容器。服务是一种功能,可在由不同方编写的组件之间进行共享。因为这个原因,您必须在使用和创建服务时沿袭某些规则。

 

  DesignSurface DesignSurfaceManager

  .NET Framework 2.0 引入的两个类,用于宿主设计器并为设计器提供服务:DesignSurface 和 DesignSurfaceManagerDesignSurface 是用户眼中的设计器;它是用户进行操作以更改设计时功能的 UIDesignSurface 可作为一个单独的设计器使用,或者可与 DesignSurfaceManager 联合使用以提供宿主多个 DesignSurfaces 的应用程序的一个公共实现。

  DesignSurface 自动提供一些设计时服务(参见MSDN)。其中的大部分服务可在服务容器中重写。替换不可替换的服务是非法的,原因是这些服务的实现均相互依赖。注意,添加到服务容器中(实现 IDisposable)的所有服务将在处置设计表面时进行处置。

  DesignSurfaceManager 旨在成为设计器的容器。它提供常规服务,用于处理设计器、属性窗口和其他全局对象之间的事件路由。DesignSurfaceManager 的使用是可选的,但建议在有若干设计器窗口的情况下使用它。

 

一个DesignSurface 的例子

// 创建一个Form的设计面
DesignSurface ds = new DesignSurface();
ds.BeginLoad(typeof(Form));
 
//获得这个设计面的试图,并在一个窗体中显示出来
Control c = ds.View as Control;
Form f = new Form();
c.Parent = f;
c.Dock = DockStyle.Fill;
f.Show();

  同样,您也能够利用任意具有可用根设计器的组件加载 DesignSurface。例如,可以加载一个 UserControl 或一个 Component 

 

  IDesignerHost (设计宿主)

  由 DesignSurface 提供的一个主要服务是 IDesignerHost。它是用于提供设计器和对类型、服务和事务进行访问的主要接口。它还可用于创建和销毁组件。要向我之前已创建的 Windows 窗体设计器添加一个按钮,只需从 DesignSurface 获得 IDesignerHost,然后用它创建如图 7 所示的按钮。 

 

IDesignerHost idh = (IDesignerHost)ds.GetService(typeof(IDesignerHost));
Button b = (Button)idh.CreateComponent(typeof(Button));// 创建组件
// Set the Parent of this Button to the RootComponent (the Form)
b.Parent = (Form)idh.RootComponent;

  工具箱

  工具箱需要实现 IToolboxService — 该服务添加到服务容器,并且可由任何需要使用它的用户访问。 

 

  DesignerLoader(设计器加载器)

  持久保持设计器,设计器加载器用于从某些持久状态载入设计器。 

  除了加载窗体设计,设计器加载器还可以保存设计。因为保存是可选操作,所以设计器加载器要进行侦听以改变设计器宿主的事件,然后自动保存与这些事件相关的状态。 

  .NET Framework 2.0 引入两个新类,用于编写自定义的加载器:BasicDesignerLoader 和 CodeDomDesignerLoader。

  1.上面,我们已经演示过通过传递组件的类型来加载 DesignSurface 的根组件。2.然而,如果您使用加载器,则它可用于加载设计表面。当使用加载器时,将使用如下所示的 BeginLoad 代码片断

// Load it using a Loader
ds.BeginLoad(new MyLoader());

  DesignerLoader 用于加载 DesignSurface 中的根组件,以及创建任意组件。创建一个新窗体或任意其他根组件时,只载入加载器。对比一下,当从代码文件或其他存储进行加载时,加载器用于分析文件或存储,重新创建根组件以及任何其他需要的组件。

  .NET Framework 定义一个名为 DesignerLoader 的抽象基类,它用于加载和保存持久存储的设计器。该基类是抽象的,因此可使用任意类型的持久性模型。然而,它也增加了该类实现的复杂性。

 

  BasicDesignerLoade

  CodeDomDesignerLoader

  BasicDesignerLoader 提供一个完整且通用的设计器加载器实现,但不包括与持久性格式相关的信息。象 DesignerLoader 一样,它是抽象的,不表示任意有关持久性格式的信息。然而,BasicDesignerLoader 所作的就是处理一些标准工作,如了解何时进行保存,了解如何重新加载,以及跟踪设计器的更改通知。它的功能还包括,支持多个加载依赖项,跟踪修改过的位以指示需要保存变更,延缓重新加载支持的空闲时间。

 

  .NET Framework 定义一个名为代码文档对象模型(Code Document Object ModelCodeDOM)的对象模型。所有源代码基本上均可拆分为基元元素,并且 CodeDOM 是这些元素的一个对象模型。当代码符合 CodeDOM 时,生成的对象模型可以稍后发送到特殊语言的代码生成器,以呈现适当的代码。 

 

  尤其强大的是,我们可以将一个窗体保存为xml(使用BasicDesignerLoader加载器),这样可以在设计窗体后反序列化。 

 

 

.NET创建宿主设计器的总结(一)

 

  根据以上.NET创建宿主设计器--DesignHostDesignSurface.可以知道宿主容器在其中扮演着重要的角色。而DesignSurface就是宿主容器:他不仅仅是一个设计面,还提供了很多的服务,设计面+这些服务=宿主容器。

 

  作为设计面

 

  将他作为一个设计面是一个最重要的功能,可以通过下面的代码:

// 创建一个Form的设计面
DesignSurface ds = new DesignSurface();
ds.BeginLoad(typeof(Form));
//获得这个设计面的视图,并在一个窗体中显示出来(设计面也是一个Control)
Control c = ds.View as Control;
Form f = new Form();
c.Parent = f;
c.Dock = DockStyle.Fill;
f.Show();

  如果我们想给设计面加菜单,或者工具条,可以通过加载服务的方式:

IServiceContainer container = ds .GetService(typeof(IServiceContainer)) as 
IServiceContainer;
menuCommandService = new MenuCommandService(surface);
if (container != null)
{
     container.AddService(typeof(IToolboxService), toolBoxService);
     container.AddService(typeof(IMenuCommandService), menuCommandService);
}

  这样,我们将ToolBox加到某种容器中(Panel)以后,便能够将工具箱和设计面关联起来了。

  以某种状态加载设计器/保存设计器

  我们可以将设计的页面保存为XML/C#/VB代码等,并且可以从这些代码文件中重新加载,并显示相应的定义设计器。这就是保存/加载设计器。

MS提供的主要有两个基础的设计器加载器(DesignerLoader):BasicDesignerLoade 和 CodeDomDesignerLoader

  这样我们就能通过某种加载器,序列化定义为相应的类型。(XML/C#/VB

 

  1.使用某种加载器:

DesignSurface ds = new DesignSurface();
BasicDesignerLoaderbasicHostLoader = new BasicDesignerLoader(typeof(Form));
hostSurface.BeginLoad(basicHostLoader);
hostSurface.Loader = basicHostLoader;

//获得这个设计面的视图,并在一个窗体中显示出来(设计面也是一个Control)
Control c = ds.View as Control;
Form f = new Form();
c.Parent = f;
c.Dock = DockStyle.Fill;
f.Show();

  设计面上的创建组件

  作为宿主容器还应该有创建组件的责任。并能够将这些组件绑定到设计面。这通过IDesignerHost 来实现,他提供设计器和对类型、服务和事务进行访问的主要接口,它还可用于创建和销毁组件。

  如下代码所示:

IDesignerHost idh = (IDesignerHost)ds.GetService(typeof(IDesignerHost));
Button b = (Button)idh.CreateComponent(typeof(Button));// 创建组件
// Set the Parent of this Button to the RootComponent (the Form)
b.Parent = (Form)idh.RootComponent;

  .NET创建宿主设计器的总结(二)

  通过查看DesignSurfaceView的代码:

通过查看DesignSurface的View的代码:
IComponent rootComponent = ((IDesignerHost) this._host).RootComponent;
 ........
 IRootDesigner designer = ((IDesignerHost) 
this._host).GetDesigner(rootComponent) as IRootDesigner;
 .........
ViewTechnology[] supportedTechnologies = 
designer.SupportedTechnologies;
int index = 0;
while (index < supportedTechnologies.Length)
{
     ViewTechnology technology = supportedTechnologies[index];
     return designer.GetView(technology);
}

  (ViewTechnology 枚举 ,定义设计器宿主所支持的技术集的标识符。一般我们使用Default, 指定默认的视图技术支持就可以了。关于IComponent 可以参考下面的两篇文档)

  1.

IComponent rootComponent = ((IDesignerHost) this._host).RootComponent;

我们可以看到,他先通过host(设计宿主)获得根组件,(比如我们设计一个顺序工作流,那么根组件就是一个SequentialActivity,如果设计一个Form,那么根组件就是一个Form

  2.

 

IRootDesigner designer = ((IDesignerHost) this._host).GetDesigner(rootComponent) as IRootDesigner;

 

这里通过host(设计宿主)的GetDesigner方法,获得了根组件的根设计器(对比前面的例子,也就是一个SequentialWorkflowDesigner或者FormDocumentDesigner

  3.这里我们做个例子,一路追踪SequentialWorkflowDesigner,最后找到了它的基类,ActivityDesigner,实现了IRootDesigner 接口,查看他的GetView代码:

 

 

object IRootDesigner.GetView(ViewTechnology technology)
{
DesignSurface service = this.GetService(typeof(DesignSurface)) as DesignSurface;
IDesignerHost host = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (((this.workflowView == null) && (service != null)) && ((host != null) && (host.RootComponent == this.Activity)))
{
this.workflowView = this.CreateView(technology);
}
return this.workflowView;
}

 

  或者FormDocumentDesigner的父类DocumentDesignerGetView

object IRootDesigner.GetView(ViewTechnology technology)
{
if ((technology != ViewTechnology.Default) && (technology != ViewTechnology.WindowsForms))
{
throw new ArgumentException();
}
return this.frame;
}

  我们可以发现,他们返回的是WorkflowView的实例或者DesignerFrame的实例,前者继承了UserControl,或者继承了Control,所以他们都能被添加到Panle子类的容器中。

  关于Host

  我们可以发现他在DesignSurface构造的时候获得:

public DesignSurface() : this((IServiceProvider) null)
{
}
public DesignSurface(IServiceProvider parentProvider)
{
this._parentProvider = parentProvider;
this._serviceContainer = new 
DesignSurfaceServiceContainer(this._parentProvider);
ServiceCreatorCallback callback = new 
ServiceCreatorCallback(this.OnCreateService);
this.ServiceContainer.AddService(typeof(ISelectionService), 
callback);
this.ServiceContainer.AddService(
typeof(IExtenderProviderService), callback);
this.ServiceContainer.AddService(
typeof(IExtenderListService), callback);
this.ServiceContainer.AddService(
typeof(ITypeDescriptorFilterService), callback);
this.ServiceContainer.AddService(
typeof(IReferenceService), callback);
this.ServiceContainer.AddService(typeof(DesignSurface), this);
this._host = new DesignerHost(this);
}

  其他的战且不管,发现Host直接被new成了DesignerHost,他是IDesignerHost的默认实现。这个我们只能查看代码,也没有相关的文档,太难分析了。通过其他方法:

  设计加载器

  我们知道我们是可以自定义设计加载器的,通常在这里面我们会自己加一些服务,比如工具条,右键菜单等。代码如下:

internal sealed class WorkflowLoader : WorkflowDesignerLoader
    {
        #region Overrides from WorkflowDesignerLoader
        protected override void Initialize()
        {
            base.Initialize();
            // Add all the services to the loaderhost
            IDesignerLoaderHost host = LoaderHost;
            if (host != null)
            {
                this.SetBaseComponentClassName("foo.Workflow1");
                host.AddService(typeof(IMenuCommandService), new 
WorkflowMenuCommandService(host));
                host.AddService(typeof(IToolboxService), new 
ToolboxService(host));
            }
        }
}

  我们前面刚刚说过,我们可以通过给设计面加服务来实现类似的操作,怎么这里有可以呢?而且,使用自定义设计器加载器的代码方式也很让人迷糊:

DesignSurface designSurface = new DesignSurface();
WorkflowLoader loader = new WorkflowLoader();
designSurface.BeginLoad(loader);

  难道在调用dsBeginLoad以后,就有某种方式调用了DesignLoaderInitialize?并且,将服务加到了ds中?

  查看designSurface.BeginLoad(loader);的代码:

internal void BeginLoad(DesignerLoader loader)
{
    this._loader = loader;
    try
    {
        this._loader.BeginLoad(this);
    }
    .......
}

  我们可以发现关键的代码是他调用了DesignerLoader 的BeginLoad,并且把自己(DesignSurface)给传递进去,通过查看WorkflowDesignerLoader 父类BasicDesignerLoader的BeginLoad

public override void BeginLoad(IDesignerLoaderHost host)
{
    if (this._host == null)
    {
        this._host = host;
        ..............
        this.Initialize();
        host.Activated += new EventHandler(this.OnDesignerActivate);
        host.Deactivated += new EventHandler(this.OnDesignerDeactivate);
    }
}

  哈哈,关键的代码发现了,我们发现BeginLoad的参数正是IDesignerLoaderHost ,并且将传进来的DesignSurface赋给了它的一个实例变量, private IDesignerLoaderHost _host; 并且调用了Initialize()方法。

  再回头看我们自定义加载器的Initialize()方法就很清楚了,我们获得了IDesignerLoaderHost (其实就是DesignSurface,并且将服务加给了DesignSurface)。

  现在再来看一下IDesignerLoaderHost 的定义:提供一个接口,该接口可扩展设计器宿主以支持从序列化状态加载。

  从字面的意思来说,IDesignerLoaderHost 是”设计器加载器的宿主“,搞不明白为什么他是DesignSurface??

  1.我们的设计器宿主(DesignHost)被包含进了设计面(DesignSurace)中。可以通过设计器宿主的GetDesigner(设计的类型(Form/Button))方法获得设计器。

IDesigner designer = ((IDesignerHost)  this._host).GetDesigner(rootComponent);

  2.获得设计器以后就能通过设计器的GetView返回该设计器的视图显示。(就是我们可以看到的设计的样子)

  3.可以通过设计宿主的 designerHost.Container.Add(activity, rootSiteName); 加入向我们的设计面中加入控件。

  4.有时候也需要保存/加载设计器,所以就有了自定义的DesignLoader(设计器加载器)

 

 

 

原文地址:https://www.cnblogs.com/code1992/p/10176074.html