zoukankan      html  css  js  c++  java
  • How to: Create an Action Using the Action Attribute 如何:使用ActionAttribute创建按钮

    This example demonstrates how to create an Action within a persistent class declaration (i.e., how to convert a persistent class method into a SimpleAction or PopupWindowShowAction).

    此示例演示如何在持久性类声明中创建操作(即,如何将持久性类方法转换为简单操作或 PopupWindowShowAction)。

    Tip 提示
    A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E3884
    完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E3884

    .

    Note 注意
    • Actions that are created by the Action attribute are not designed to operate when there are no selected objects. The attribute's ActionAttribute.SelectionDependencyType parameter can be set either to RequireSingleObject or RequireMultipleObjects (see MethodActionSelectionDependencyType). To create an Action that does not require the selected object, add a Controller and implement an Action in it (see Add a Simple Action and Add an Action that Displays a Pop-up Window).
    • 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
    • 在没有选定对象时,由 Action 属性创建的操作不设计为操作。属性的 action属性.选择依赖类型参数可以设置为"要求单个对象"或"需要多个对象"(请参阅方法操作选择依赖类型)。要创建不需要所选对象的操作,请添加控制器并在其中实现操作(请参阅添加简单操作和添加显示弹出窗口的操作)。
    • 移动平台不支持本主题中描述的方法。如果需要在移动应用程序中实施此方案,请使用支持中心与我们联系

    Create A Simple Action

    创建简单操作

    Let us start with the following Task business class.

    让我们从以下 Task 业务类开始。

    [DefaultClassOptions, ImageName("BO_Task"), DefaultProperty(nameof(Subject))]
    public class Task : BaseObject {
        public Task(Session session) : base(session) { }
        private string subject;
        public string Subject {
            get { return subject; }
            set { SetPropertyValue(nameof(Subject), ref subject, value); }
        }
        private bool isCompleted;
        public bool IsCompleted {
            get { return isCompleted; }
            set { SetPropertyValue(nameof(IsCompleted), ref isCompleted, value); }
        }
    }

    Assume that implementing an Action that changes the IsCompleted value to true is required. Add the following Complete method to the Task class and decorate it with the ActionAttribute attribute.

    假设需要实现将"已完成"值更改为 true 的操作。将以下 Complete 方法添加到 Task 类,并使用 ActionAttribute 属性装饰它。

    [Action(Caption="Complete", TargetObjectsCriteria = "Not [IsCompleted]")]
    public void Complete() {
        IsCompleted = true;
    }

    Such methods are automatically collected from business classes by the ObjectMethodActionsViewController Controller. This controller will create the Task.Complete Action, which will invoke the Complete method for each selected Task. Note the use of the ActionAttribute.TargetObjectsCriteria parameter. The Action will be disabled for Tasks that are already completed (the IsCompleted property is true). You can pass other parameters to customize the Action's look and behavior. The image below illustrates the Complete Action in the UI.

    这些方法由 ObjectMethodActionsView 控制器自动从业务类收集。此控制器将创建 Task.完成操作,该操作将为每个选定的任务调用"完成"方法。请注意使用 Action属性.目标对象标准参数。对于已完成的任务,将禁用该操作("已完成"属性为 true)。您可以传递其他参数来自定义操作的外观和行为。下图说明了 UI 中的"完成操作"。

    Create an Action that Displays the Pop-up Dialog

    创建显示弹出对话框的操作

    Next, extend the Task class demonstrated in the previous section of this topic with several additional properties to demonstrate a more complex scenario. Here are these properties (Deadline and Comments).

    接下来,使用几个附加属性扩展本主题上一节中演示的 Task 类,以演示更复杂的方案。下面是这些属性(截止日期和注释)。

    using DevExpress.ExpressApp.Model;
    // ...
    private DateTime? deadline;
    public DateTime? Deadline {
        get { return deadline; }
        set { SetPropertyValue(nameof(Deadline), ref deadline, value); }
    }
    private string comments;
    [Size(SizeAttribute.Unlimited), ModelDefault("AllowEdit", "False")]
    public string Comments {
        get { return comments; }
        set { SetPropertyValue(nameof(Comments), ref comments, value); }
    }

    Assume that you are required to implement an Action that postpones the Deadline for a specified number of days and updates the Comments text. First, implement the following non-persistent class that declares parameters to be specified when an end-user postpones a Task.

    假设您需要实施一个操作,该操作将截止日期推迟指定天数并更新注释文本。首先,实现以下非持久性类,该类声明最终用户推迟任务时要指定的参数。

    using DevExpress.ExpressApp.DC;
    // ...
    [DomainComponent]
    public class PostponeParametersObject {
        public PostponeParametersObject() { PostponeForDays = 1; }
        public uint PostponeForDays { get; set; }
        [Size(SizeAttribute.Unlimited)]
        public string Comment { get; set; }
    }

    Then, add the following Postpone method to the Task class. This method takes a parameter of the PostponeParametersObject type and is decorated with the Action attribute.

    然后,将以下"推迟"方法添加到任务类。此方法采用"延迟参数对象"类型的参数,并带有 Action 属性进行修饰。

    [Action(Caption = "Postpone",
        TargetObjectsCriteria = "[Deadline] Is Not Null And Not [IsCompleted]")]
    public void Postpone(PostponeParametersObject parameters) {
        if (Deadline.HasValue && !IsCompleted && (parameters.PostponeForDays > 0)) {
            Deadline += TimeSpan.FromDays(parameters.PostponeForDays);
            Comments += String.Format("Postponed for {0} days, new deadline is {1:d}
    {2}
    ",
            parameters.PostponeForDays, Deadline, parameters.Comment);
        }
    }

    As a result, the Task.Postpone Action, which accompanies the Task Views, will be automatically created. This Action invokes a dialog with the PostponeParametersObject Detail View and executes the Postpone method after an end user specifies the parameters and clicks OK. The following images illustrate this.

    因此,将自动创建任务视图附带的任务.延迟操作。此操作调用具有"延迟参数对象详细信息视图"的对话框,并在最终用户指定参数并单击"确定"后执行"推迟"方法。下图说明了这一点。

    ActioAttributeExample_PostponeActionWin

    ActioAttributeExample_PostponeActionWeb

    Tip 提示
    If there is a requirement for reordering parameters displayed in a popup dialog, modify the layout of a parameter object's Detail View in the Model Editor. In the example above, the appropriate Detail View node is PostponeParametersObject_DetailView. Refer to the View Items Layout Customization topic to learn more about layout customizations.
    如果需要在弹出对话框中显示重新排序参数,请修改模型编辑器中参数对象"详细信息视图"的布局。在上面的示例中,PostponeParametersObject_DetailView相应的"详细信息视图"节点。请参阅查看项目布局自定义主题,了解有关布局自定义的详细信息。

    Access the current object instance

    访问当前对象实例

    If you declare a PostponeParametersObject constructor that takes a parameter of a target business objects' type (Task), the current Task instance will be passed to this constructor when the Action is executed.

    如果声明一个采用目标业务对象类型(Task)参数的 Stosto 参数的 Stosto 参数,则在执行操作时,将传递给此构造函数。

    [DomainComponent]
    public class PostponeParametersObject {
        private Task task;
        public PostponeParametersObject(Task task) {
            PostponeForDays = 1;
            this.task = task;
        }
        // ...
    }
  • 相关阅读:
    jchdl
    jchdl
    jchdl
    jchdl
    jchdl
    jchdl
    jchdl
    UVa 437 (变形的LIS) The Tower of Babylon
    UVa 1025 (动态规划) A Spy in the Metro
    UVa 10129 (并查集 + 欧拉路径) Play on Words
  • 原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Create-an-Action-Using-the-Action-Attribute.html
Copyright © 2011-2022 走看看