• Initialize a Property After Creating an Object 创建对象后初始化属性 (XPO)


    In this lesson, you will learn how to set the default value for a particular property of a business class. For this purpose, the Priority property will be added to the DemoTask class created in the Set a Many-to-Many Relationship (XPO) lesson. To initialize it, the AfterConstruction method will be overridden in this class.

    在本课中,您将学习如何为 Business 类的特定属性设置默认值。为此,优先级属性将添加到在"设置多对多关系 (XPO)"一课中创建的 DemoTask 类中。要初始化它,将重写此类中的"构建后"方法。

    Note

    Before proceeding, take a moment to review the following lessons:

    注意

    在继续之前,请花点时间复习以下课程:

    • Inherit from the Business Class Library Class (XPO)
    • Set a Many-to-Many Relationship (XPO)
    • Add the Priority property to the DemoTask class and declare the Priority enumeration, as shown below:

    • 从 Business Class  继承(XPO)

    • 设置多对多关系 (XPO)

    将"优先级"属性添加到 DemoTask 类并声明优先级枚举,如下所示:

    public class DemoTask : Task {
        // ...
        private Priority priority;
        public Priority Priority {
            get { return priority; }
            set {
                SetPropertyValue(nameof(Priority), ref priority, value);
            }
        }
        //...
    }
    public enum Priority {
        Low = 0,
        Normal = 1,
        High = 2
    }
    • To initialize the newly added Priority property when a DemoTask object is created, override the AfterConstruction method, as shown below:

    • 要在创建 DemoTask 对象时初始化新添加的"优先"属性,请重写 After 构造方法,如下所示:

      [DefaultClassOptions]
      [Custom("Caption", "Task")]
      public class DemoTask : Task {
          //...
          public override void AfterConstruction() {
              base.AfterConstruction();
              Priority = Priority.Normal;
          }
          //...
      }
      

      This method will be executed when the new DemoTask object is created. As a result, the Priority property will be initialized with the specified value. For details on the AfterConstruction method, refer to the PersistentBase.AfterConstruction Method topic in the XPO documentation.

              创建新的 DemoTask 对象时,将执行此方法。因此,将用指定的值初始化"Priority"属性。有关后构造方法的详细信息,请参阅 XPO 文档中的持久基础.构建后方法主题。 

    • Run the WinForms or ASP.NET application. Create a new DemoTask object by selecting DemoTask in the drop-down list of the New (new_dropdown_btn) button. (In the Detail View that represents the newly created DemoTask object, note that the Priority property is set to Normal, as declared in the code above.) Notice that the enumeration property is automatically displayed by the combo box editor.

    • 运行 WinForms 或ASP.NET应用程序。通过在"新建(new_dropdown_btn)"按钮的下拉列表中选择"演示任务",创建新的"演示任务"对象。(在表示新创建的 DemoTask 对象的详细信息视图中,请注意,优先级属性设置为"正常",如上述代码中声明的那样。请注意,枚举属性由组合框编辑器自动显示。

      Tutorial_BMD_Lesson12_1

    You can see the code demonstrated in this lesson in the MySolution.Module | Business Objects | DemoTask.cs (DemoTask.vb) file of the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%DocumentsDevExpress Demos 19.2ComponentseXpressApp FrameworkMainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/您可以在 MySolution.

    模块中看到本课中演示的代码。业务对象 |DemoTask.cs (DemoTask.vb) 文件的主演示安装与 XAF.默认情况下,主演示应用程序安装在 %PUBLIC%_文档_DevExpress 演示 19.2_组件_eXpressApp 框架_MainDemo 中。ASP.NET版本可在 http://demos.devexpress.com/XAF/MainDemo/ 在线获取


  • 相关阅读:
    第四章 数组和方法
    JAVA基础笔记
    ios navigationcontroller add multi navigationItem
    Objective-C Runtime能做什么?
    在Xcode中使用Git进行源码版本控制
    分析iOS Crash文件:符号化iOS Crash文件的3种方法
    Core Animation Layer(22)
    Touch Events and UIResponder(19)
    UIPopoverController and Modal View Controllers(13)
    Subclassing UITableViewCell(15)
  • 原文地址:https://www.cnblogs.com/foreachlife/p/InitializeProperty.html
Copyright © 2020-2023  润新知