简体   繁体   English

DelegateCommand.CanExecute方法行为不正确

[英]DelegateCommand.CanExecute method not behaving correctly

I'm binding a control to a DelegateCommand and the CanExecute portion of it is not working properly. 我将控件绑定到DelegateCommand,并且其CanExecute部分无法正常工作。 I am using the Prism libraries. 我正在使用Prism库。 Can anyone tell me why? 谁能告诉我为什么?

Command declaration and instantiation: 命令声明和实例化:

public PlayerManagementViewModel(DatabaseManager dbManager)
{
    _dbManager = dbManager;
    this.ResetUpToDateStatusCommand = new DelegateCommand(() => this.ResetXpUpToDateStatus());
    this.DeletePlayerCommand = new DelegateCommand(() => this.DeleteSelectedPlayer(), () => SelectedPlayer != null);
    this.RefreshPlayers();
}

public ICommand DeletePlayerCommand { get; private set; }

SelectedPlayer definition: SelectedPlayer定义:

public Player SelectedPlayer
{
    get { return _selectedPlayer; }
    set
    {
        SetProperty(ref this._selectedPlayer, value);
        this.OnPropertyChanged(() => this.FormattedPlayerStatus);
    }
}

The weird thing is that if you look at the line above the DeletePlayerCommand instantiation, that line works just fine. 奇怪的是,如果您查看DeletePlayerCommand实例化上方的DeletePlayerCommand行,那行就可以了。 I don't get any CanExecute behavior out of it, but at least it works. 我没有得到任何CanExecute行为,但至少它能起作用。 As is, the DeletePlayerCommand command never fires off, even with a breakpoint, unless I remove the CanExecute portion of the constructor entirely. DeletePlayerCommand ,即使没有断点, DeletePlayerCommand命令也永远不会触发,除非我完全删除了构造函数的CanExecute部分。

Can anyone please explain to me why this is or what I'm doing wrong? 谁能向我解释这是为什么还是我做错了什么?

If the CanExecute function of the DeletePlayerCommand is () => SelectedPlayer != null , then there must be a DelegateCommandBase.RaiseCanExecuteChanged Method call when SelectedProperty value is changed: 如果CanExecute的功能DeletePlayerCommand() => SelectedPlayer != null ,则必须有一个DelegateCommandBase.RaiseCanExecuteChanged方法打电话时SelectedProperty值改变:

Raises CanExecuteChanged on the UI thread so every command invoker can requery to check if the command can execute. 在UI线程上引发CanExecuteChanged,以便每个命令调用者都可以重新查询以检查命令是否可以执行。

The appropriate UI-element (with data-bound command) is a command invoker. 适当的UI元素(带有数据绑定命令)是命令调用程序。

To summarize, the implementation of the SelectedPlayer property should be updated as follows: 总而言之, SelectedPlayer属性的实现应更新如下:

class PlayerManagementViewModel : BindableBase
{
    private Player _selectedPlayer;
    private readonly DelegateCommand _deletePlayerCommand;

    public PlayerManagementViewModel(...)
    {
        _deletePlayerCommand = new DelegateCommand(() => DeleteSelectedPlayer(), () => SelectedPlayer != null);
    }

    public ICommand DeletePlayerCommand
    {
        get { return _deletePlayerCommand; }
    }

    public Player SelectedPlayer
    {
        get { return _selectedPlayer; }
        set
        {
            SetProperty(ref _selectedPlayer, value);
            OnPropertyChanged(() => FormattedPlayerStatus);
            _deletePlayerCommand.RaiseCanExecuteChanged();
        }
    }
}

It is the way PRISM DelegateCommnd is designed. 这是PRISM DelegateCommnd的设计方式。 refer CanExecuteChanged event of ICommand . 请参阅ICommand的CanExecuteChanged事件

Alternatively you can derive the DelegateCommand to overcome the limitation. 或者,您可以派生DelegateCommand来克服该限制。 refer the below code. 请参考以下代码。

 class DelegateCmdEx : DelegateCommand
{       
    public DelegateCmdEx(Action executeMethod):base(executeMethod)
    {

    }

    public DelegateCmdEx(Action executeMethod, Func<bool> canExecuteMethod)
        : base(executeMethod, canExecuteMethod)
    {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 MVVM 中使用 DelegateCommand 的异步 CanExecute 方法 - Async CanExecute method using DelegateCommand in MVVM DelegateCommand 的 CanExecute 逻辑 - CanExecute Logic for DelegateCommand DelegateCommand <T> 与另一个CanSee函数 <T,bool> 除了CanExecute - DelegateCommand<T> with another CanSee func<T,bool> except CanExecute Prism DelegateCommand 的 CanExecute 在 RaiseCanExecuteChanged 后获取 null 参数 - Prism DelegateCommand's CanExecute getting null parameter after RaiseCanExecuteChanged DelegateCommand的重载方法 - Overloaded method for DelegateCommand Prism中的DelegateCommand,其canExecuteMethod由ObservableCollection中的属性确定。 我如何继续“观察” canExecute? - DelegateCommand in Prism whose canExecuteMethod is determined by a property in an ObservableCollection. How do I continue to “Observe” canExecute? 如何让 DelegateCommand CanExecute 使用 MVVM 更新 WPF UI? - How do I get DelegateCommand CanExecute to update WPF UI using MVVM? 正确使用CanExecute用于MVVM Light ICommand - Correctly Using CanExecute for MVVM Light ICommand 链接CanExecute命令方法和授权管理 - Linking CanExecute method of commands and authorization management 如何在 WPF 上使用 ICommand 中的 CanExecute 方法 - How to use the CanExecute Method from ICommand on WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM