CanExecuteChanged 的​​用途是什么?

发布于 2024-10-09 07:57:49 字数 68 浏览 0 评论 0原文

我可以使用 CanExecuteChanged 更改“可以执行”条件吗?

或者...它的用途是“做什么”?

Can I use CanExecuteChanged to change the "can execute" condition?

Or else... "for what" its used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若能看破又如何 2024-10-16 07:57:49

不,您不能使用它来更改可以执行状态。它是一个事件,参与 ICommand 模式的对象可以选择监听此事件,例如按钮可以使用此事件来知道何时重新查询命令状态(通过调用 canexecute 方法) ) 设置其启用状态。

为了使可以执行模式发挥作用,需要有一些东西可以用来引发事件。 Prism 的 DelegateCommand 有一个方法,您可以调用该方法来手动引发此事件,因此订阅者如果选择加入该模式,将重新查询可以执行方法。

  • 将命令分配给按钮。
  • 按钮订阅可以执行更改事件。
  • 按钮执行可以执行方法,它返回 false - 禁用按钮。
  • 您更改可以执行方法所依赖的状态。
  • 您调用 raise 可以在 Prism 命令上执行更改。
  • 可以执行发生更改的事件。
  • 按钮事件处理程序触发。
  • 按钮调用命令可以执行方法 - 按钮启用。

示例

在以下基于 Prism 的示例中,我们在执行保存命令时将 SaveCommand CanExecute 的状态从 false 更改为 true。对 RaiseCanExecuteChanged 的调用将导致引发 CanExecuteChanged 事件,并导致客户端调用 CanExecute 方法。实际上,这将使绑定到 SaveCommand 的“保存”按钮将其状态从启用更改为禁用,然后再次更改回启用。

public class BlingViewModel
{
    private DelegateCommand<object> _saveCommand;
    private bool _canSaveExecute = true;

    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand == null)
            {
                _saveCommand = new DelegateCommand<object>
                    (
                    executeMethod: _ => Save()
                    ,
                    canExecuteMethod: _ => _canSaveExecute
                    );
            }
            return _saveCommand;
        }
    }

    private void Save()
    {
        _canSaveExecute = false;
        _saveCommand.RaiseCanExecuteChanged();

        Console.WriteLine("Saving...");

        _canSaveExecute = true;
        _saveCommand.RaiseCanExecuteChanged();
    }
}

No you can not use it to change the can execute state. It is an event and objects which participate in the ICommand pattern can choose to listen to this event e.g. a button may use this event to know when to re-query the commands state (by calling the can execute method) to set its enabled state.

In order for the can execute pattern to be useful there needs to be something that can be used to raise the event. Prism's DelegateCommand has a method you can call to manually raise this event so subscribers will re-query the can execute method if they have opted into the pattern.

  • Assign command to button.
  • Button subscribes to can execute changed event.
  • Button execute can execute method and it returns false - disables button.
  • You change state that can execute method depends on.
  • You call raise can execute changed on Prism command.
  • Can execute changed event is raised.
  • Button event handler fires.
  • Button calls command can execute method - button is enabled.

Example

In the following Prism based example we change the state of SaveCommand CanExecute from false to true whilst the save command is executing. The call toRaiseCanExecuteChanged will cause the CanExecuteChanged event to be raised, and clients to call the CanExecute method. In practice this would make a Save button that was bound to SaveCommand change its state from enabled to disabled and back to enabled again.

public class BlingViewModel
{
    private DelegateCommand<object> _saveCommand;
    private bool _canSaveExecute = true;

    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand == null)
            {
                _saveCommand = new DelegateCommand<object>
                    (
                    executeMethod: _ => Save()
                    ,
                    canExecuteMethod: _ => _canSaveExecute
                    );
            }
            return _saveCommand;
        }
    }

    private void Save()
    {
        _canSaveExecute = false;
        _saveCommand.RaiseCanExecuteChanged();

        Console.WriteLine("Saving...");

        _canSaveExecute = true;
        _saveCommand.RaiseCanExecuteChanged();
    }
}
别把无礼当个性 2024-10-16 07:57:49

或者只需调用 System.Windows.Input.CommandManager.InvalidateRequerySuggested() 以便重新评估您的 CanExecute 处理程序。

Or just call System.Windows.Input.CommandManager.InvalidateRequerySuggested() so that your CanExecute handlers are re-evaluated.

掩耳倾听 2024-10-16 07:57:49
public override event EventHandler CanExecuteChanged
{    
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}

我不知道这对性能有何影响;然而,无论如何,它们似乎都不太伟大。这就是我用的。

public override event EventHandler CanExecuteChanged
{    
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}

I don't know what the performance implications are for this; however, they don't seem to be too great, either way. This is what I use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文