This project has moved. For the latest updates, please go here.
7

Closed

RelayCommand<T>.CanExecute with null parameter returns false always (since 5.0)

description

When I call CanExecute on a RelayCommand<T> with the parameter null, the behavior has changed going from 4.4.32 to 5.0.2.
I have decompiled the assemblies and write the results below. It is clear that the 4.4.32 code accepted null as an argument, whereas the 5.0.2 code does not.

This has broken our code in various places.
// Type: GalaSoft.MvvmLight.Command.RelayCommand`1
// Assembly: GalaSoft.MvvmLight, Version=4.4.32.16316, Culture=neutral, PublicKeyToken=6bdae7d54059775e
// MVID: 594A8629-B383-43DD-A41F-7AABF57F1F20
// Assembly location: D:\source\RhinoPlugins\obj\win64\packages\MvvmLightLibs.4.4.32.7\lib\net40\GalaSoft.MvvmLight.dll

    public bool CanExecute(object parameter)
    {
      if (this._canExecute == null)
        return true;
      if (!this._canExecute.IsStatic && !this._canExecute.IsAlive)
        return false;
      if (parameter == null && typeof (T).IsValueType)
        return this._canExecute.Execute(default (T));
      else
        return this._canExecute.Execute((T) parameter);
    }

// Type: GalaSoft.MvvmLight.Command.RelayCommand`1
// Assembly: GalaSoft.MvvmLight, Version=5.0.2.32240, Culture=neutral, PublicKeyToken=0e453835af4ee6ce
// MVID: 589788DB-9532-473D-82DB-8D64C2AA450B
// Assembly location: D:\source\RhinoPlugins\obj\win64\packages\MvvmLightLibs.5.0.2.0\lib\net40\GalaSoft.MvvmLight.dll
    
    public bool CanExecute(object parameter)
    {
      if (this._canExecute == null)
        return true;
      if (this._canExecute.IsStatic || this._canExecute.IsAlive)
      {
        if (parameter == null && typeof (T).IsValueType)
          return this._canExecute.Execute(default (T));
        if (parameter is T)
          return this._canExecute.Execute((T) parameter);
      }
      return false;
    }
Closed Feb 15, 2015 at 5:14 PM by lbugnion

comments

saturne_1606 wrote Nov 18, 2014 at 7:14 AM

Same situation here.

CanExecute doesn't raise :
<Button Content="Se déconnecter" Command="{Binding LogoutCommand}" />
CanExecute raise :
<Button Content="Se déconnecter" Command="{Binding LogoutCommand}" CommandParameter="{Binding ElementName=elt}" />

saturne_1606 wrote Nov 18, 2014 at 8:57 AM

It would be :
public bool CanExecute(object parameter)
    {
      if (this._canExecute == null)
        return true;
      if (this._canExecute.IsStatic || this._canExecute.IsAlive)
      {
        if (parameter == null && typeof (T).IsValueType)
          return this._canExecute.Execute(default (T));
        if (parameter is T || parameter == null)
          return this._canExecute.Execute((T) parameter);
      }
      return false;
    }

lbugnion wrote Nov 18, 2014 at 9:21 AM

This bug has been reported by multiple users, apologies for the regression that came in from the PCL branch. Will fix in the next minor release.

lbugnion wrote Feb 6, 2015 at 2:32 PM

Fixed in V5.1 (upcoming)

lbugnion wrote Feb 6, 2015 at 4:12 PM

DLLs are available here:
http://1drv.ms/1ylP1TS

Nuget will be updated ASAP.