How to: Deactivate (Hide) an Action in Code 如何:在代码停用(隐藏)按钮
An Action's visibility is managed by the ActionBase.Active property. When this property returns false, an action is invisible. Follow the steps below to deactivate a predefined or custom action. In the current example, the Delete action will be disabled.
操作的可见性由 ActionBase.Active 属性管理。当此属性返回 false 时,操作是不可见的。按照以下步骤停用预定义或自定义操作。在当前示例中,将禁用"删除"操作。
- Create the DeactivateDeleteController Controller. To learn how to do this, refer to the Controller Class article.
-
Override its OnActivated method as shown below. This method is executed after the Action's activation and raises the Controller.Activated event.
-
创建停用控制器控制器。要了解如何执行此操作,请参阅控制器类一文。
-
覆盖其 On 已激活方法,如下所示。此方法在操作激活后执行,并引发控制器。激活事件。
public partial class DeactivateDeleteController : ObjectViewController { private const string Key = "Deactivation in code"; DeleteObjectsViewController DeleteController; public DeactivateDeleteController() { InitializeComponent(); } protected override void OnActivated() { base.OnActivated(); DeleteController = Frame.GetController<DeleteObjectsViewController>(); if(DeleteController != null) { DeleteController.Active[Key] = !(View.ObjectTypeInfo.Type == typeof(Contact) && View is ListView); } } protected override void OnDeactivated() { if(DeleteController != null) { DeleteController.Active.RemoveItem(Key); DeleteController = null; } base.OnDeactivated(); } }
In the code above, a false or true value is added to the Active BoolList of the DeleteObjectsViewController. As a result, the Controller's Actions will be hidden from the Views for which the false value is added (Contact List Views in this example). This approach is similar to the Controller's ViewController.TargetObjectType and ViewController.TargetViewType properties, but they are not applicable here because enabling a disabled controller does not occur automatically on a View change.
在上面的代码中,将假值或真值添加到删除对象视图控制器的活动 BoolList 中。因此,控制器的操作将从添加错误值的视图(本示例中的联系人列表视图)中隐藏。此方法类似于控制器的视图控制器.TargetTototo 和 ViewController.TargetType.TargetViewType 属性,但它们在这里不适用,因为启用禁用的控制器不会在视图更改时自动发生。
Note 注意
XAF provides other ways to deactivate an action that might be more appropriate for your scenario. Refer to the ActionBase.Active topic to learn more.
Refer to the Determine an Action's Controller and Identifier topic to learn how to determine which Controller provides the Action to be hidden or deactivated.
To learn about the ways to access the existing controller's properties in code, refer to the Customize Controllers and Actions article.
XAF 提供了其他方法来停用可能更适合您的方案的操作。请参阅 ActionBase.活动主题以了解更多信息。
请参阅确定操作的控制器和标识符主题,了解如何确定哪个控制器提供要隐藏或停用的操作。
要了解在代码中访问现有控制器属性的方法,请参阅自定义控制器和操作一文。
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客户端