Add a Simple Action using an Attribute 使用特性添加简单按钮
In the previous Add a Simple Action lesson, you learned how to add an Action by implementing the View Controller. There is another approach that may be more convenient when the Action is intended for a certain business class. In this lesson, you will learn how to add a Simple Action using an attribute. For this purpose, a new method will be added to the DemoTask class, and the ActionAttribute attribute will be applied to it.
在前面的"添加简单按钮"课中,您学习了如何通过实现视图控制器来添加操作。当 Action 适用于特定业务类时,还有另一种方法可能更方便。在本课中,您将学习如何使用属性添加简单操作。为此,新方法将添加到 DemoTask 类中,并将 Action属性属性应用于该方法。
Note 注意
Before proceeding, take a moment to review the following lessons.
在继续之前,请花点时间复习以下课程。
- Set a Many-to-Many Relationship (XPO/EF)
- Add a Simple Action
-
Add the Postpone method to the DemoTask class as shown below.
-
设置多对多关系 (XPO/EF)
-
添加简单操作
- 将"推迟"方法添加到演示任务类,如下所示。
[DefaultClassOptions] [ModelDefault("Caption", "Task")] public class DemoTask : Task { //... [Action(ToolTip = "Postpone the task to the next day")] public void Postpone() { if(DueDate == DateTime.MinValue) { DueDate = DateTime.Now; } DueDate = DueDate + TimeSpan.FromDays(1); } }
- The Postpone method sets the Task.DueDate property to the next date, after the date previously specified. Since the ActionAttribute attribute is used, the Postpone button will be displayed in the UI, and the Postpone method will be called when clicking this button.
- "延迟"方法将 Task.DueDate 属性设置到下一个日期,该日期是以前指定的日期之后。由于使用了 ActionAttribute 属性,因此"推迟"按钮将显示在 UI 中,单击此按钮时将调用"推迟"方法。
-
Run the WinForms or ASP.NET application. Select the Task item in the navigation control. Show the Due Date column using the Column Chooser. To activate the Column Chooser, right-click the Task List View header. The Column Chooser allows end users to show or hide columns at runtime, by dragging a column header to or from the table header.
运行 WinForms 或ASP.NET应用程序。在导航控件中选择"任务"项。使用列选择器显示到期日期列。要激活列选择器,请右键单击"任务列表视图"标头。列选择器允许最终用户在运行时通过将列标题拖动到表标题或从表标题中显示或隐藏列。
- Select one or more Task objects in the Task List View. Find the Postpone toolbar button, which represents the Postpone Action that you have implemented above. Click this button. The DueDate property of the selected objects displayed in the Due Date column will be modified.
-
在"任务列表"视图中选择一个或多个"任务"对象。查找"推迟"工具栏按钮,该按钮表示您在上面实现的"推迟操作"。单击此按钮。将修改"到期日期"列中显示的选定对象的"过期日期"属性。
Tip 提示
When the browser window shrinks, some Actions become hidden and can be accessed using the "..." button (see IModelActionWeb.AdaptivePriority).
当浏览器窗口收缩时,某些操作将变为隐藏状态,可以使用"...""按钮进行访问(请参阅 IModelActionWeb.自适应优先级)。
Note 注意
You can also use the Action attribute to implement an action that asks an end user to specify parameters in a popup dialog (e.g., the number of days to postpone a Task ). Refer to the How to: Create an Action Using the Action Attribute topic to see an example.
您还可以使用 Action 属性实现一个操作,要求最终用户在弹出对话框中指定参数(例如,推迟任务的天数)。请参阅"如何:使用操作属性创建操作"主题以查看示例。
You can see the code demonstrated here 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%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/
您可以在 MySolution.模块 |业务对象 |DemoTask.cs (DemoTask.vb) 文件的主演示安装与 XAF.主演示应用程序安装在%PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/
.
XAF开发成品案例参考
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
作者博客: http://www.cnblogs.com/foreachlife
欢迎加入CIIP框架\XAF技术应用交流群: 336090194 群文件中有更多相关工具及文档资料
转载请注明出处。多谢!
欢迎加我微信: admiralcn 或扫码:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端