To learn how to use CanExecute , take a look at Silverlight or WPF - there are many blogs that talk about how to use ICommand - for example. http://weblogs.asp.net/nmarun/archive/2009/12/02/using-icommand-silverlight-4.aspx or http://blog.galasoft.ch/archive/2009/09/26/using- relaycommands-in-silverlight-and-wpf.aspx
An example would be something like:
private MvxRelayCommand _disconnectCommand; public IMvxCommand DisconnectCommand { get { if (_disconnectCommand == null) _disconnectCommand = new MvxRelayCommand(this.GetService<IConnectionService>().Disconnect, item => this.IsItemConnected(item)); return _disconnectCommand; } } private void SomeServiceNotificationHandler() { _disconnectCommand.RaisePropertyChanged(); } private bool IsItemConnected(object thing) { return ; }
There is one small problem though ....
CanExecute is actually not fully implemented in all MvxBindings on all platforms ... It will work for some of them, but for some of them it will not - and I really don't know which ones are currently! If you encounter problems, then please let me know (via GitHub questions), and they will be fixed ...
Personally ... I am not inclined to use CanExecute - instead, I prefer to use a separate boolean property, which then binds to any property available on the control. most controls have something like Enabled , IsEnabled , Disabled , IsDisabled , etc.
I usually find it easier (and easier to read) to set a boolean property rather than calling RaiseCanExecuteChanged
eg. I would use something like:
<Button android:id="@+id/ButtonConnect" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Disconnect" local:MvxBind="{'Click':{'Path':'DisconnectCommand'},'Enabled':{'Path':'UsbConnected'}}" />
You can definitely argue that the CanExecute approach has its advantages - because it stores Command logic in a single object and because it can be used to prevent Execute calls that occur inside RelayCommand . That's why I am happy to try to fix CanExecute errors in mvvmcross bindings when we find them.
Stuart
source share