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.活动主题以了解更多信息。
请参阅确定操作的控制器和标识符主题,了解如何确定哪个控制器提供要隐藏或停用的操作。
要了解在代码中访问现有控制器属性的方法,请参阅自定义控制器和操作一文。

猜你喜欢

转载自www.cnblogs.com/foreachlife/p/How-to-Deactivate-Hide-an-Action-in-Code.html