How to: Distribute Custom Templates with Modules 如何:使用模块分发自定义模板

The eXpressApp Framework uses default Templates when constructing Windows Forms UIs. You can customize them. Approaches for customization are defined in the Template Customization and How to: Create a Custom WinForms Ribbon Template topics. Once you have developed a custom Template, you may need to use it in several applications. The appropriate way to distribute Windows Forms Templates is to add them to a module that will then be added to the required Windows Forms applications. This topic demonstrates how to do this. ASP.NET templates can be easily distributed, as is. You can add them to an ASP.NET application project replacing the defaults.

eXpressApp 框架在构造 Windows 窗体 U 时使用默认模板。您可以自定义它们。自定义方法在模板自定义和如何:创建自定义 WinForms 功能区模板主题中定义。开发自定义模板后,可能需要在多个应用程序中使用它。分发 Windows 窗体模板的适当方法是将它们添加到模块中,然后将该模块添加到所需的 Windows 窗体应用程序中。本主题演示如何执行此操作。ASP.NET模板可以像现在一样轻松分发。您可以将它们添加到替换默认值ASP.NET应用程序项目中。

Note 注意
The approach described in this topic is not supported by the Mobile platform. If it is necessary to implement this scenario in your Mobile application, contact us using the Support Center
移动平台不支持本主题中描述的方法。如果需要在移动应用程序中实施此方案,请使用支持中心与我们联系

.

When constructing a Windows Forms UI, the WinApplication class instance uses its Frame Template Factory to create a Template that is appropriate in the current context. A Frame Template Factory is a class that implements the IFrameTemplateFactory interface. This interface exposes a single method, CreateTemplate, that gets the current Template context as a parameter. The eXpressApp Framework has a base class that implements this interface, FrameTemplateFactoryBase, and its descendant, DefaultLightStyleFrameTemplateFactory. The base class exposes abstract methods that are called by the CreateTemplate method, dependent on the passed Template context. They are: CreateNestedFrameTemplate, CreatePopupWindowTemplate, CreateLookupControlTemplate, CreateLookupWindowTemplate, CreateApplicationWindowTemplate and CreateViewTemplate. The DefaultLightStyleFrameTemplateFactory class overrides these methods to create the default XAF Templates.

构造 Windows 窗体 UI 时,WinApplication 类实例使用其框架模板工厂创建适合当前上下文的模板。框架模板工厂是实现 IFrame模板工厂接口的类。此接口公开单个方法 CreateTemplate,该方法将当前模板上下文作为参数获取。eXpressApp 框架具有实现此接口的基类 FrameTemplateFactoryBase 及其后代 DefaultLightStyleFrame模板工厂。基类公开由 CreateTemplate 方法调用的抽象方法,具体取决于传递的模板上下文。它们是:创建嵌套框架模板、创建弹出窗口模板、创建查找控制模板、创建查找窗口模板、创建应用程序窗口模板和创建视图模板。默认LightStyleFrame模板工厂类将覆盖这些方法以创建默认的XAF模板。

To make an application use custom Templates, do the following:

  • Add the custom Templates to the module project to be distributed.
  • Implement a Frame Template Factory class in the module to be distributed.

要使应用程序使用自定义模板,请执行以下操作:

  • 将自定义模板添加到要分发的模块项目中。

  • 在要分发的模块中实现框架模板工厂类。

This class should return the required custom Template in an appropriate context. The code below demonstrates how to implement this for two custom Templates: the MyMainForm Template, which is created to represent the main Window, and the MyDetailViewForm Template, which is created to represent a detail form. These templates have custom constructors taking an IModelTemplate object as the only parameter, for initialization purposes. The newly implemented MyFrameTemplateFactory class is inherited from the DefaultLightStyleFrameTemplateFactory class, to override the CreateApplicationWindowTemplate and CreateViewTemplate methods only.

此类应在适当的上下文中返回所需的自定义模板。下面的代码演示如何为两个自定义模板实现此参数:为表示主窗口而创建的 MyMainForm 模板和为表示详细信息窗体而创建的 MyDetailViewForm 模板。出于初始化目的,这些模板具有以 IModelTemplate 对象为唯一参数的自定义构造函数。新实现的 MyFrame模板工厂类是从 DefaultLightStyleFrame模板工厂类继承的,以仅重写创建应用程序窗口模板和创建视图模板方法。

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Utils;
using DevExpress.ExpressApp.Win;
//...
public class MyFrameTemplateFactory : DefaultLightStyleFrameTemplateFactory {
    private WinApplication application;
    public MyFrameTemplateFactory(WinApplication application) {
        Guard.ArgumentNotNull(application, nameof(application));
        this.application = application;
    }
    protected IModelTemplate GetTemplateInfo(TemplateContext templateContext) {            
        return application.Model.Templates[templateContext.Name];
    }
    protected override DevExpress.ExpressApp.Templates.IFrameTemplate           
        CreateApplicationWindowTemplate() {            
        return new MyMainForm(GetTemplateInfo(TemplateContext.ApplicationWindow));
    }
    protected override DevExpress.ExpressApp.Templates.IFrameTemplate CreateViewTemplate() {
        return new MyDetailViewForm(GetTemplateInfo(TemplateContext.View));
    }
}
  • Set the custom Frame Template Factory for the application.

  • 为应用程序设置自定义框架模板工厂。

To make the application use the custom Frame Template Factory to create Templates, set it for the WinApplication.FrameTemplateFactory property in the Setup method of the distributed module. The following code demonstrates this:

要使应用程序使用自定义框架模板工厂创建模板,请为 WinApplication.FrameTemplateFactory 属性在分布式模块的安装程序方法中设置模板。以下代码演示了这一点:

using DevExpress.ExpressApp.Win;
//...
public class MyWindowsFormsModule : ModuleBase {
   public override void Setup(XafApplication application) {
      base.Setup(application);
      ((WinApplication)application).FrameTemplateFactory = 
         new MyFrameTemplateFactory((WinApplication)application);
   }
   //...
}
  • Compile the module project and add it to the required application project (see Application Solution Structure).
  • 编译模块项目并将其添加到所需的应用程序项目中(请参阅应用程序解决方案结构)。
原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Distribute-Custom-Templates-with-Modules.html