|
In VB.NET I do this:
Public Property LogoutCommand As ICommand
...
LogoutCommand = New RelayCommand(AddressOf LogoutExecute, AddressOf CanLogoutExecute)
but when I try this in C#:
public ICommand LogoutCommand;
...
LogoutCommand = new RelayCommand(LogoutExecute, CanLogoutExecute);
...
private void LogoutExecute(){...}
private bool CanLogoutExecute()
{
return true;
}
...
I get an error: "the best overloaded method match for RelayCommand(System.Action<object>, System.Predicate<object>) has some invalid arguments"
Here is the RelayCommand Class:
public class RelayCommand : ICommand
{
private readonly Action<Object> _execute;
private readonly Predicate<Object> _canExecute;
public RelayCommand(Action<object> execute): this(execute, null){ }
public RelayCommand(Action<object> execute, Predicate<Object> canExecute)
{
if (execute == null)
{
throw new ArgumentException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
[DebuggerStepThrough()]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
public class RelayCommand<T> : ICommand
{
private readonly Action<T> _execute;
private readonly Predicate<T> _canExecute;
public RelayCommand(Action<T> execute): this(execute, null){}
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
{
if (execute == null)
{
throw new ArgumentException("execute");
}
}
[DebuggerStepThrough()]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute((T)parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute((T) parameter);
}
}
Any suggestions?
I don't speak Idiot - please talk slowly and clearly
"I have sexdaily. I mean dyslexia. Fcuk!"
Driven to the arms of Heineken by the wife
modified 18-Nov-12 3:54am.
|
|
|
|
|
Andy_L_J wrote: AddressOf
... in your VB declarations is one clue here along with your error message.
Your RelayCommand constructor takes two delegates. In VB.Net you use the AddressOf operator to get a delegate for a method. In C# we use a delegate directly.
Try something like this to create delegates to your methods:
LogoutCommand = new RelayCommand(new Action<object>(LogoutExecute), new Predicate<object>(CanLogoutExecute));
Edit: Your LogoutExecute and CanLogoutExecute methods should both have one parameter of type object .
modified 18-Nov-12 4:32am.
|
|
|
|
|
Cheers Dave, worked a treat.
I am self-learning C# after many years with VB.NET and some of conversions are making me crazy.
I don't speak Idiot - please talk slowly and clearly
"I have sexdaily. I mean dyslexia. Fcuk!"
Driven to the arms of Heineken by the wife
|
|
|
|
|
Keep persevering! Most things are fairly straight forward in my experience - I was once a VBer too, many years ago. Now I actually find it quite difficult to code in VB for the first 15 minutes or so, mainly cause I want to put a ; at the end of everything and wrap things in { }
|
|
|
|
|
I am enjoying the process however at the moment I get a couple hunderd moans from the compiler about missing ; and { } every time I build
I don't speak Idiot - please talk slowly and clearly
"I have sexdaily. I mean dyslexia. Fcuk!"
Driven to the arms of Heineken by the wife
|
|
|
|