【问题标题】:Making Command 1-time-executable使命令 1-time-executable
【发布时间】:2018-06-22 18:30:23
【问题描述】:

最好的做法是让MVVM 中的Prism 框架中的DelegateCommand 只能执行一次,以防止可能导致应用程序崩溃的点击垃圾邮件按钮。 非常感谢!

【问题讨论】:

标签: mvvm prism delegatecommand canexecute


【解决方案1】:

这是我通常做的:

  • 您应该拥有要删除的内容的属性。使用 CanExecute
  • 在您的委托命令中,观察属性是否为空?使用
  • DelegateCommand 中的 ObserveProperty 并将其设置为 What you are
  • 正在删除。在您的 DeleteCommandExecute 中,在删除后将该属性设置为 null。

这是一个例子

    private Class object;
    public Class Object
    {
        get { return object; }
        set { SetProperty(ref object, value); }
    }
    private DelegateCommand _delete;
    public DelegateCommand Delete =>
        _delete ?? (_delete = new DelegateCommand(ExecuteDelete, CanExecuteDelete)).ObservesProperty(()=> Object);

    void ExecuteDelete()
    {
        MyServices.Delete(Object);
        Object = null;
    }

    bool CanExecuteDelete()
    {
        return Object != null;
    }

【讨论】:

  • 在 void ExecuteDelete() 之后说 CanExecuteDelete = false 而不是使用 ObservesCanExecute(()=>CanExecuteDelete) 代替 ObservesProperty 不是更好吗? Ty 为您解答!
  • 如果您使用 CanExecuteDelete= false 则您将无法单击该命令。仅当对象引发通知时,提供的答案才会检查 canExecuteMethod。这就是为什么我在 ExecuteCommand 中将对象设置为 null,这将引发一个通知,这将导致命令检查 CanExecute。
猜你喜欢
  • 2012-05-30
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 2014-11-02
相关资源
最近更新 更多