学习008-02-02-01 Add a Simple Action(添加简单操作)

Add a Simple Action(添加简单操作)

This lesson explains how to create a Simple Action — a button that executes custom code when a user clicks it.
本课介绍如何创建简单操作-当用户单击它时执行自定义代码的按钮。

The instructions below demonstrate how to add the Clear tasks button to the Employee Detail View. A click on this button clears all Tracked Tasks of the selected Employee.
下面的说明演示了如何将清除任务按钮添加到员工详细信息视图。单击此按钮会清除所选员工的所有跟踪任务。

Note
Before you proceed, take a moment to review the previous lessons:
在继续之前,请花点时间回顾一下之前的课程:

  • Implement a Data Model: Basics
  • Implement Reference Properties
  • Configure a Many-to-Many Relationship

Step-by-Step Instructions(分步说明)

1.Add a View Controller. In the Solution Explorer, right-click the Controllers folder in the MySolution.Module project and choose Add DevExpress Item | New Item… to invoke the Template Gallery. Select the XAF Controllers | View Controller Visual Studio template, specify ClearContactTasksController as the new item’s name, and click Add Item.
添加视图控制器。在解决方案资源管理器中,右键单击MySolutions. Module项目中的控制器文件夹,然后选择添加DevExpress项目|新建项目…以调用模板库。选择XAF控制器|视图控制器Visual Studio模板,将ClearContactTasksController指定为新项目的名称,然后单击添加项目。
在这里插入图片描述

2.Visual Studio displays an autogenerated ClearContactTasksController.cs file with a single View Controller declaration. In the controller constructor, specify the controller properties:
Visual Studio显示带有单个View Controller声明的自动生成的ClearContactTasksController. cs文件。在控制器构造函数中,指定控制器属性:

C#

// ...
using MySolution.Module.BusinessObjects;

// ...
public partial class ClearContactTasksController : ViewController {
    public ClearContactTasksController() {
        InitializeComponent();
        //Activate the Controller only in the Detail View
        TargetViewType = ViewType.DetailView;
        //Specify the type of objects that can use the Controller
        TargetObjectType = typeof(Employee);
    }
// ...
}

If you do not specify the TargetObjectType property, the application displays the controller’s actions for all Detail Views.
如果您不指定TargetObjectType属性,应用程序将显示控制器对所有详细信息视图的操作。

Tip
You can also create a generic ViewController or ObjectViewController<ViewType, ObjectType> object and specify the target type as the ViewType parameter. For more information on how to customize a controller’s functionality, refer to the following topic: Define the Scope of Controllers and Actions.
您还可以创建通用ViewController或ObjectViewController<ViewType, ObjectType>对象,并将目标类型指定为ViewType参数。有关如何自定义控制器功能的更多信息,请参阅以下主题:定义控制器和操作的范围。

3.Add a new action to the controller and a handler for the action’s Execute event:
向控制器添加新操作和操作的Execute事件的处理程序:

C#

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using MySolution.Module.BusinessObjects;
// ...
public partial class ClearEmployeeTasksController : ViewController {
    public ClearEmployeeTasksController() {
        InitializeComponent();
        TargetViewType = ViewType.DetailView;
        TargetObjectType = typeof(Employee);


        SimpleAction clearTasksAction = new SimpleAction(this, "ClearTaskAction", PredefinedCategory.View) {
            //Specify the Action's button caption.
            Caption = "Clear tasks",
            //Specify the confirmation message that pops up when a user executes an Action.
            ConfirmationMessage = "Are you sure you want to clear the Tasks list?",
            //Specify the icon of the Action's button in the interface.
            ImageName = "Action_Clear"
        };
        //This event fires when a user clicks the Simple Action control. Handle this event to execute custom code.
        clearTasksAction.Execute += ClearTasksAction_Execute;
    }
    private void ClearTasksAction_Execute(Object sender, SimpleActionExecuteEventArgs e) {
        while(((Employee)View.CurrentObject).DemoTasks.Count > 0) {
            ((Employee)View.CurrentObject).DemoTasks.Remove(((Employee)View.CurrentObject).DemoTasks[0]);
            ObjectSpace.SetModified(View.CurrentObject, View.ObjectTypeInfo.FindMember(nameof(Employee.DemoTasks)));
        }
    }
// ...
}

You can use one of the standard images or import your own to customize the Action button’s icon.
您可以使用标准图像之一或导入您自己的图像来自定义操作按钮的图标。

4.Run the application.
运行应用程序。

Open a Detail View for an Employee item. Link several tasks to this item and save it.
打开员工项目的详细信息视图。将多个任务链接到此项目并保存它。

Click the Clear tasks button. A confirmation message appears. Click OK to remove all Tasks from the current Employee.
单击清除任务按钮。出现一条确认消息。单击确定从当前员工中删除所有任务。

ASP.NET Core Blazor
在这里插入图片描述

Windows Forms
在这里插入图片描述

You can display an action’s button in a Detail View layout instead of a toolbar. Refer to the following topic for more information: How to: Include an Action in a Detail View Layout.
您可以在详细视图布局中而不是工具栏中显示操作的按钮。有关详细信息,请参阅以下主题:如何:在详细视图布局中包含操作。

Note
CodeRush allows you to add Actions and Controllers with a few keystrokes. To learn about the Code Templates for XAF, refer to the following help topic: XAF Templates.
CodeRush允许您通过几次击键添加操作和控制器。要了解XAF的代码模板,请参阅以下帮助主题:XAF模板。

Next Lesson(下一课)

Add a Parametrized Action
添加参数化操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值