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 属性,但它们在这里不适用,因为启用禁用的控制器不会在视图更改时自动发生。