简体   繁体   中英

Odd behaviour onCanExecute RelayCommand MVVMLight 5+

I'm migrating a tool from MVVM Light 4.0.3 to 5.4.1 and I have found a very odd issue with the newest RelayCommand implementation.

This is the old implementation in the V4.0.3 :

IMG1

IMG2

This is the newest implementation in the V5.4.1 :

IMG4

IMG3

Before I was able to use variables to define the canExecute behaviour (enabled a button) with the following code:

public ICommand GetNewItemsFromDB { get; private set; }

private bool _IsActive;
public bool IsActive
{
    get
    {
        return _IsActive;
    }
    set
    {
        if (_IsActive != value)
        {
            _IsActive = value;
            this.RaisePropertyChanged(() => IsActive);
        }
    }
}

GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => { return IsActive == false; });

private void GetDataFromDB()
{
    IsActive = true;
}

The previous code was able to enable the button without any issue in the MVVM Light 4.0.3; however, in the newest implementation is always disabled, I added changed a bit since there is a new definition of keepTargetAlive :

GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => { return IsActive == false; }, true);

Also, I tried the false option and nothing changed. The only way that I found to re-enabled it, it was to set a predefined value like this one:

GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => true, true);

This implementation is going to be useless in my case since the RelayCommand depends on the variable IsActive , which determines if it's enabled or not. Does anyone what I should change in the V5 to make it work? Thanks for your suggestions.

If I am understanding this correctly.

If you are using this class in WPF4.5 or above, you need to use the GalaSoft.MvvmLight.CommandWpf namespace (instead of GalaSoft.MvvmLight.Command ). This will enable (or restore) the CommandManager class which handles automatic enabling/disabling of controls based on the CanExecute delegate.

And in the release notes:

Important note about issue 7659 : In order to fix the issue where the controls are not disabled anymore depending on the state of the RelayCommand.CanExecute delegate, you need to make a small change into your code. To opt-in into the fixed behavior, please change the namespace you are using from GalaSoft.MvvmLight.Command to GalaSoft.MvvmLight.CommandWpf .

I remember correctly, somewhere in ancient history I had to do this myself for a project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM