The event 'Command.CanExecuteChanged' can only appear on the left hand side of += or -=

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



The event 'Command.CanExecuteChanged' can only appear on the left hand side of += or -=



Am I missing something in my Command-ViewModel?


public class Command : ICommand

public Command(Action execute, Func<bool> canExecute = null)

this.execute = execute;
this.canExecute = canExecute ?? new Func<bool>(() => true);

private Action execute;
private Func<bool> canExecute;
public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)

return canExecute.Invoke();


public void Execute(object parameter)

execute?.Invoke();




Everytime I want to use CanExecuteChanged in my MainViewModel with this line of code ((Command)MyCommand).CanExecuteChanged(); it gives me this error The event 'Command.CanExecuteChanged' can only appear on the left hand side of += or -=


CanExecuteChanged


MainViewModel


((Command)MyCommand).CanExecuteChanged();


The event 'Command.CanExecuteChanged' can only appear on the left hand side of += or -=




4 Answers
4



CanExecuteChanged is an event. You can only use it like this:


CanExecuteChanged


YourCommand.CanExecuteChanged += (sender, args) =>

// Do your magic here
;



To answer your question yes you are missing something from your code. I can't tell if you are using the Command class supplied by Xamarin.Forms but if you aren't then you really should be!


Command


Xamarin.Forms



Ultimately you cannot interact with an event outside of the class that it belongs to apart from subscribing for the event notification which is what the 'The event 'Command.CanExecuteChanged' can only appear on the left hand side of += or -=' is telling you. To subscribe you implement something like the following:


event


MyCommand.CanExecuteChanged += (sender, e) =>

// Your code to react to the event goes here.
;



What you can do though and this is where you need to be using the Xamarin.Forms Command class (or you can implement something similar yourself in your Command class). Is call ChangeCanExecute e.g.


Xamarin.Forms


Command


Command


ChangeCanExecute


((Command)MyCommand).ChangeCanExecute();



This will then trigger the event to be fired and thus updating any UI controls that are bound to that command.





I'm now using Xamarin.Forms... with ((Command)MyCommand).ChangeCanExecute(); makes it so much easier. Now it is working fine. Thanks.
– paulvoelker
Aug 10 at 7:11


((Command)MyCommand).ChangeCanExecute();


public event EventHandler CanExecuteChanged;



is syntactic sugar. What the compiler actually generates when you put this in is something like*


private EventHandler _CanExecuteChanged;
public event EventHandler CanExecuteChanged

add _CanExecuteChanged += value;
remove _CanExecuteChanged -= value;



So the CanExecuteChange that's publicly exposed isn't the actual field but only something you can add and remove handlers with.


CanExecuteChange



Related note: The backing field being private is also the reason for the normal pattern of having a protected OnXXXX() method in the base class.


private


protected


public event EventHandler CanExecuteChanged;
protected void OnCanExecuteChanged(EventArgs args)

CanExecuteChanged?.Invoke(this, args);



*Note the "like" part; there's some null checking that's needed for proper add and remove as well.



I deleted my Command-Class and now I am using the Xamarin.Forms Command Class which is making this a lot easier because now I can just use this delicious short line of code: ((Command)MyCommand).ChangeCanExecute(); to fire the event.


Command-Class


Xamarin.Forms Command Class


((Command)MyCommand).ChangeCanExecute();



Thanks to all of you guys.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS