How to: Create Additional ListView Nodes in Code using a Generator Updater 如何:使用生成器更新程序在代码中创建其他列表视图节点

By default, two ListView nodes are generated in the Application Model for each business class. These nodes represent a general-purpose List View, and a Lookup List View that contains fewer columns (see List View Column Generation). Often, it is required to add more List Views manually. These additional List Views can be used as View Variants, Dashboard items, etc. Typically, this task can be accomplished in the Model Editor. However, in certain scenarios, adding nodes in code may be required. The default process of generating Views node's child nodes is handled by the built-in ModelViewsNodesGenerator Nodes Generator. To customize this process, you should "attach" a Generator Updater class to this Generator. This topic describes how to implement a Generator Updater that creates custom ListView nodes. Additionally, an Updater that creates View Variants using these new nodes is illustrated. For details on Nodes Generators and Generator Updaters, refer to the Extend and Customize the Application Model in Code topic.

默认情况下,每个业务类的应用程序模型中生成两个 ListView 节点。这些节点表示通用列表视图和包含较少列的查找列表视图(请参阅列表视图列生成)。通常,需要手动添加更多列表视图。这些附加列表视图可用作视图变体、仪表板项等。通常,此任务可以在模型编辑器中完成。但是,在某些情况下,可能需要在代码中添加节点。生成视图节点子节点的默认过程由内置的 ModelViewsNode 发电机生成器处理。要自定义此过程,应将生成器更新器类"附加到"此生成器。本主题介绍如何实现创建自定义 ListView 节点的生成器更新程序。此外,还演示了使用这些新节点创建视图变体的更新程序。有关节点生成器和生成器更新器的详细信息,请参阅代码中的扩展和自定义应用程序模型主题。

Note 注意
Mobile applications do not support the View Variant, so the approach described in this topic is not supported by the Mobile platform.
移动应用程序不支持视图变体,因此移动平台不支持本主题中描述的方法。

Create List Views

创建列表视图

Let us consider the following Employee business class.

让我们考虑以下员工商务舱。

[DefaultClassOptions, ImageName("BO_Person")]
public class Employee : BaseObject {
    public Employee(Session session) : base(session) { }
    private string firstName;
    private string lastName;
    private string position;
    private string email;
    public string FirstName {
        get { return firstName; }
        set { SetPropertyValue(nameof(FirstName), ref firstName, value); }
    }
    public string LastName {
        get { return lastName; }
        set { SetPropertyValue(nameof(LastName), ref lastName, value); }
    }
    public string FullName {
        get { return String.Format("{0} {1}", FirstName, LastName); }
    }
    public string Position {
        get { return position; }
        set { SetPropertyValue(nameof(Position), ref position, value); }
    }
    public string Email {
        get { return email; }
        set { SetPropertyValue(nameof(Email), ref email, value); }
    }
}

The List View generated by default for this class is illustrated below.

下面说明了默认情况下为此类生成的列表视图。

GeneratorUpdater_DefaultListView

This List View is defined by the Views | Employee_ListView node. Let us implement a Generator Updater that generates two additional List Views, which are:

  1. A List View with hidden FirstName and LastName columns (Employee_ListView_FewColumns);
  2. A List View where records are grouped by the Position column (Employee_ListView_Grouped).

此列表视图由视图定义 |Employee_ListView节点。让我们实现一个生成器更新程序,该更新程序生成两个额外的列表视图,它们是:

  1. 具有隐藏名字和姓氏列的列表视图(Employee_ListView_FewColumns);
  2. 列表视图,其中记录按"位置"列 (Employee_ListView_Grouped) 进行分组。

A Generator Updater class should inherit the abstract ModelNodesGeneratorUpdater<T> class, where T is the type of the Nodes Generator to be customized. A built-in Nodes Generator that generates the Views node content is the ModelViewsNodesGenerator. The abstract ModelNodesGeneratorUpdater`1.UpdateNode method must be implemented in a custom Generator Updater. The parameter of the ModelNode type, passed to this method, represents a generated node. In our case, this node is the Views node. To add child nodes, the ModelNode.AddNode<T> method can be used. Note that the IModelObjectView.ModelClass property must be specified for each ListView node. After that, the ListView node properties can be accessed and their values can be customized. To access the Columns node, use the IModelListView.Columns property. The following snippet illustrates the AddListViewNodesGeneratorUpdater Generator Updater class.

生成器更新器类应继承抽象的模型节点发电机更新器<T>类,其中 T 是要自定义的节点生成器的类型。生成视图节点内容的内置节点生成器是模型视图节点生成器。抽象模型节点发电机更新器'1.UpdateNode方法必须在自定义生成器更新器中实现。传递给此方法的 ModelNode 类型的参数表示生成的节点。在我们的例子中,此节点是视图节点。要添加子节点,可以使用 ModelNode.AddNode<T> 方法。请注意,必须为每个 ListView 节点指定 IModelObjectView.ModelClass 属性。之后,可以访问 ListView 节点属性,并可以自定义其值。要访问列节点,请使用 IModelListView.列属性。以下代码段演示了 AddlistView 节点发电机更新器更新器更新器类。

using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Model.Core;
using DevExpress.ExpressApp.Model.NodeGenerators;
// ...
public class AddListViewNodesGeneratorUpdater : 
    ModelNodesGeneratorUpdater<ModelViewsNodesGenerator> {
    public const string FewColumnsListViewNodeIdSuffix = "_FewColumns";
    string[] columnsToHideIds = { "FirstName", "LastName" };
    public const string GroupedListViewNodeIdSuffix = "_Grouped";
    public const string GroupByColumnId = "Position";
    static Type targetType = typeof(Employee);
    public override void UpdateNode(ModelNode viewsNode) {
        AddFewColumnsListViewNode(viewsNode);
        AddGroupedListViewNode(viewsNode);
    }
    public static IModelListView GetDefaultListView(ModelNode viewsNode) {
        return viewsNode.Application.BOModel.GetClass(targetType).DefaultListView;
    }
    IModelListView AddListViewNode(ModelNode viewsNode, string listViewId) {
        IModelListView listViewNode = (IModelListView)viewsNode.AddNode<IModelListView>(listViewId);
        listViewNode.ModelClass = viewsNode.Application.BOModel.GetClass(targetType);
        return listViewNode;
    }
    void AddFewColumnsListViewNode(ModelNode viewsNode) {
        string nodeId = GetDefaultListView(viewsNode).Id + FewColumnsListViewNodeIdSuffix;
        IModelListView fewColumnsListViewNode = AddListViewNode(viewsNode, nodeId);
        IModelColumns columns = (IModelColumns)fewColumnsListViewNode.Columns;
        foreach (string columnId in columnsToHideIds) {
            columns[columnId].Index = -1;
        }
    }
    void AddGroupedListViewNode(ModelNode viewsNode) {
        string nodeId = GetDefaultListView(viewsNode).Id + GroupedListViewNodeIdSuffix;
        IModelListView groupedListViewNode =
            AddListViewNode(viewsNode, nodeId);
        IModelColumns columns = (IModelColumns)groupedListViewNode.Columns;
        columns[GroupByColumnId].GroupIndex = 1;
    }
}

You can place this class in a separate code file in the module project, or add it to the Module.cs (Module.vb) file. The implemented Generator Updater should be registered in the overridden ModuleBase.AddGeneratorUpdaters method in the following manner:

您可以将此类放在模块项目中的单独代码文件中,或将其添加到Module.cs (Module.vb) 文件中。实现的生成器更新程序应在重写的 ModuleBase.AddgeneratorUpdaters 方法中注册,方式如下:

using DevExpress.ExpressApp.Model.Core;
// ...
public sealed partial class CreateNodesInCodeModule : ModuleBase {
    // ...
    public override void AddGeneratorUpdaters(ModelNodesGeneratorUpdaters updaters) {
        base.AddGeneratorUpdaters(updaters);
        updaters.Add(new AddListViewNodesGeneratorUpdater());
    }
}

After implementing and registering the Generator Updater, rebuild the solution and invoke the Model Editor. The new ListView nodes are illustrated below.

实现并注册生成器更新程序后,重新生成解决方案并调用模型编辑器。下面说明了新的 ListView 节点。

GeneratorUpdater_ModelEditor_NewNodes

Note 注意
Your custom nodes and properties are not marked in bold font in the Model Editor, because these changes are generated in code. You can check the Model.DesignedDiffs.xafml source to ensure that it contains no customizations related to the generated nodes.
您的自定义节点和属性不会在模型编辑器中以粗体字标记,因为这些更改是在代码中生成的。您可以检查 Model.设计Diffs.xafml源,以确保它不包含与生成的节点相关的自定义项。

Create View Variants

创建视图变体

Let us implement View Variants for Employee List View to make List Views created in the previous section of this topic visible in the UI. Although the View Variants are typically designed in the Model editor, it is also possible to create View Variants in code, via the Generator Updater. The Generator Updater that generates View Variants using Views created by the AddListViewNodesGeneratorUpdater is illustrated in the snippet below. The View Variants module should be added before implementing this class (see Provide Several View Variants for End-Users).

让我们实现员工列表视图的视图变体,以使在本主题的上一部分中创建的列表视图在 UI 中可见。尽管视图变体通常是在模型编辑器中设计的,但也可以通过生成器更新程序在代码中创建视图变体。使用 AddlistViewNode 发电机更新器创建的视图生成变体的生成器更新器如下代码段所示。在实现此类之前,应添加视图变体模块(请参阅为最终用户提供多个视图变体)。

using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Model.Core;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.ViewVariantsModule;
// ...
class AddViewVariantsGeneratorUpdater : ModelNodesGeneratorUpdater<ModelViewsNodesGenerator> {
    public override void UpdateNode(ModelNode viewsNode) {
        IModelView rootView = AddListViewNodesGeneratorUpdater.GetDefaultListView(viewsNode);
        IModelView fewColumnsListView = 
            (IModelView)viewsNode.GetNode(rootView.Id +
            AddListViewNodesGeneratorUpdater.FewColumnsListViewNodeIdSuffix);
        IModelView groupedListView =
            (IModelView)viewsNode.GetNode(rootView.Id +
            AddListViewNodesGeneratorUpdater.GroupedListViewNodeIdSuffix);
        AddVariant("Default", "Default", rootView, rootView, true);
        AddVariant("FewColumns", "Few Columns", rootView, fewColumnsListView, false);
        AddVariant("Grouped", "Grouped", rootView, groupedListView, false);
    }
    void AddVariant(string variantId, string caption, 
        IModelView rootView, IModelView variantView, bool isCurrent) {
        IModelVariants variants = ((IModelViewVariants)rootView).Variants;
        IModelVariant variant = variants.AddNode<IModelVariant>(variantId);
        variant.View = variantView;
        variant.Caption = caption;
        if (isCurrent) variants.Current = variant;
    }
}

This Generator Updater should also be registered in the module's AddGeneratorUpdaters method.

此生成器更新程序也应在模块的 AddgeneratorUpdaters 方法中注册。

public sealed partial class CreateNodesInCodeModule : ModuleBase {
    // ...
    public override void AddGeneratorUpdaters(ModelNodesGeneratorUpdaters updaters) {
        base.AddGeneratorUpdaters(updaters);
        updaters.Add(new AddListViewNodesGeneratorUpdater());
        updaters.Add(new AddViewVariantsGeneratorUpdater());
    }
}

After implementing and registering the Generator Updater, rebuild the solution and invoke the Model Editor for the module project. The new Variant nodes are illustrated below.

实现并注册生成器更新程序后,重新生成解决方案并调用模块项目的模型编辑器。下面说明了新的变体节点。

GeneratorUpdater_ModelEditor_NewVariants

Now you can test the implemented Views and View Variants at runtime.

现在,您可以在运行时测试实现的视图和视图变体。

  • Windows Forms Application:

  • Windows 窗体应用程序:

    GeneratorUpdater_WinApp

  • ASP.NET Application:

  • ASP.NET应用程序:

    GeneratorUpdater_WebApp

You can use a similar approach to create Generator Updaters for built-in or custom Nodes Generators.

您可以使用类似的方法为内置或自定义节点生成器创建生成器更新程序。

原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Create-Additional-ListView-Nodes-in-Code-using-a-Generator-Updater.html