PART 1
What i normally do is something like this:
SGTryAgainCommand = new Command(
execute: SGTryAgain,
canExecute: SGTryAgainCanExecute);
where
private void SGTryAgain()
{
// Do something
}
and
private Boolean SGTryAgainCanExecute()
{
// Evaluate if command can execute!
}
If you move SGTryAgainCanExecute() inside SGTryAgain() like this:
private void SGTryAgain()
{
if (SGTryAgainCanExecute())
SetInstanceOfSyncGateway();
}
then you might be able to achieve what you are looking for, and call simply
SGTryAgainCommand = new Command(SGTryAgain);
WARNING
I would strongly discourage you from doing that since it would have some disadvantages like:
- If you bind the
Command to a Button, then not setting explicitly canExecute will cause the Button to be enabled even if it can't execute. That is a nice behavior from Button, acctually: to be able to appear disabled if canExecute returns false.
PART 2
How to fix the error you are getting
As for the error you are getting, you can change your code as follows:
OKCommand = new Xamarin.Forms.Command(execute: (x)=>OK(), canExecute: OKCanExecute);
Why are you getting that error?
The Command constructor has two overloads
Command(Action<object> execute, Func<object, bool> canExecute)
Command(Action execute, Func<bool> canExecute)
In your code, since the function you are using for canExecute has signature
// Takes an object and returns a boolean
Func<object, bool> canExecute)
you are implicitly opting for the overload where your functions take an argument, and thus you are forced to accept an argument in your execute method! So you have to write either
// just ignore the x object...
OKCommand = new Xamarin.Forms.Command(execute: (x)=>OK(), canExecute: OKCanExecute);
as mentioned above, or you have to modify your OK method signature and define like
private void OK(object x)
{
throw new NotImplementedException();
}
in which case you would be able to write again
OKCommand = new Xamarin.Forms.Command(execute: OK, canExecute: OKCanExecute);