【问题标题】:ICommands and the "CanExecuteMethod" of a DelegateCommand Why doesn't it work for us?ICommands 和 DelegateCommand 的“CanExecuteMethod” 为什么它对我们不起作用?
【发布时间】:2010-11-29 12:12:25
【问题描述】:

我们正在尝试使用 ICommand 在 Silverlight 中通过 Prism 设置按钮。我们希望偶尔禁用该按钮。 DelegateCommand 有 2 个参数,一个“ExecuteMethod”和一个“CanExecuteMethod”

当我们设置ICommand时,我们期望如果使用“CanExecuteMethod”,那么它会被调用,看是否可以调用“ExecuteMethod”。按钮的启用状态应该反映“CanExecuteMethod”的结果

我们实际看到的: 创建表单时,将调用该方法并启用或禁用按钮。 (在这种情况下,启用) CanExecuteMethod 永远不会再次被调用,即使我们尝试设置行为以防止这种情况发生,Execute 方法也会触发。执行被抛出(我们想要避免的)。

显而易见的答案是我们应该调用某种:

OnPropertyChanged("SaveCommand");

但不知何故,我们做错了。要么我们假设它的工作方式并非如此,要么我们错过了一个步骤。有任何想法吗?

代码:

SaveCommand = new DelegateCommand<string>(OnSaveCommand, CanSave);

public void OnSaveCommand( string helpNumber )
        {
            OnPropertyChanged("SaveCommand");
           //DoSaveStuff
        }

        public bool CanSave(Object sender)
        {
            return Model.CanSave();// true or false depending
        }

【问题讨论】:

    标签: silverlight mvvm prism


    【解决方案1】:

    您的 SaveCommand,因为它是一个 DelegateCommand,所以有一个名为 RaiseCanExecuteChanged() 的函数。

    当您调用此函数时,它将通过 CanSave 函数刷新控件。

    DelegateCommands 的 OnPropertyChanged 等于 MyCommand.RaiseCanExecuteChanged。

    玩得开心!

    【讨论】:

    • 我的 DelegateCommand 实现没有 RaiseCanExecuteChanged 方法,但现在多亏了它;)正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 2021-07-10
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    相关资源
    最近更新 更多