DelegateCommand 的 CanExecute 逻辑 [英] CanExecute Logic for DelegateCommand

查看:20
本文介绍了DelegateCommand 的 CanExecute 逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:焦点变成了 MVVM 而不是实际的问题,所以我正在更新它.

Update: The focus became MVVM instead of the actual question so I'm updating it.

DelegateCommandCanExecute 有问题.在我调用 RaiseCanExecuteChanged 之前它没有更新,这是期望的行为吗?

I'm having a problem with CanExecute for DelegateCommand. It doesn't update before I call RaiseCanExecuteChanged, is this the desired behavior?

我在这里上传了一个重现此问题的简单示例项目:http://dl.dropbox.com/u/39657172/DelegateCommandProblem.zip

I uploaded a simple sample project reproducing this problem here : http://dl.dropbox.com/u/39657172/DelegateCommandProblem.zip

问题是这样的,我有两个这样的Buttons.一个是将 Command 绑定到 RelayCommand 实现,另一个是绑定到 DelegateCommand

The problem is this, I have two Buttons like this. One is Binding Command to a RelayCommand implementation and the other is binding to the Prism implementation of DelegateCommand

<Button Command="{Binding DelegateSaveCommand}"/>
<Button Command="{Binding RelaySaveCommand}"/>

ViewModel ICommands

DelegateSaveCommand = new DelegateCommand(Save, CanSaveDelegate);
RelaySaveCommand = new RelayCommand(param => Save(), param => CanSaveRelay);

CanExecute 方法/谓词

public bool CanSaveDelegate()
{
    return HasChanges;
}
public bool CanSaveRelay
{
    get { return HasChanges; }
}

两者都使用属性 HasChanges.当 HasChanges 更新时,只有 CanSaveRelay 更新.这就是它的本意吗?

Both are using the property HasChanges. When HasChanges is updated, only the CanSaveRelay updates. Is this the way it's meant to be?

推荐答案

正如已经提到的,这是 DelagateCommand 的预期行为,而不是错误.DelegateCommand 不会自动引发 CanExecuteChanged 事件,您必须在适当时通过调用 RaiseCanExecuteChanged 手动引发该事件.而 RelayCommand 为此中继 CommandManager.RequerySuggested 事件.每次用户单击某处或按下按钮时都会引发此事件.

As it already was mentioned, this is intended behavior of DelagateCommand, not a bug. DelegateCommand doesn't raise CanExecuteChanged event automatically, you have to raise that event manually by calling RaiseCanExecuteChanged when appropriate. Whereas RelayCommand relays on CommandManager.RequerySuggested event for that. This event is raised every time the user clicks somewhere or presses a button.

对于不太方便或没有适当位置调用 RaiseCanExecuteChanged 的情况(例如在您的场景中,您必须在模型上订阅 PropertyChanged 事件,等)我创建了以下简单的包装器,确保包装命令的 CanExecute 方法在 CommandManager.RequerySuggested 事件上自动执行:

For situations when it is not very convenient or there is no appropriate place for calling RaiseCanExecuteChanged (like in your scenario you have to subscribe to PropertyChanged event on the model, etc) I have created the following simple wrapper that ensures that the CanExecute method of the wrapped command is executed automatically on CommandManager.RequerySuggested event:

public class AutoCanExecuteCommandWrapper : ICommand
{
    public ICommand WrappedCommand { get; private set; }

    public AutoCanExecuteCommandWrapper(ICommand wrappedCommand)
    {
        if (wrappedCommand == null) 
        {
            throw new ArgumentNullException("wrappedCommand");
        }

        WrappedCommand = wrappedCommand;
    }

    public void Execute(object parameter)
    {
        WrappedCommand.Execute(parameter);
    }

    public bool CanExecute(object parameter)
    {
        return WrappedCommand.CanExecute(parameter);
    }

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

你可以这样使用它:

DelegateSaveCommand = new AutoCanExecuteCommandWrapper(new DelegateCommand(Save, CanSaveDelegate));

这篇关于DelegateCommand 的 CanExecute 逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆