I have created an application in WPF using the MVVM pattern.
The application is running fine in the Visual Studio debugger, but when I'm running the exe from the debug/release folder, it becomes very slow.
Here is my RelayCommand
class:
public class RelayCommand : ICommand
{
private readonly Action<object> execute;
private readonly Predicate<object> canExecute;
public RelayCommand(Action<object> execute) : this(execute, DefaultCanExecute)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
bool res = false;
if (canExecute != null)
{
res = canExecute(parameter);
}
return res;
}
public void Execute(object parameter)
{
execute(parameter);
}
private static bool DefaultCanExecute(object parameter)
{
return true;
}
}
If I remove the CanExcecute()
method from my RelayCommand
class, then the exe version runs as normal.
Please can anyone explain why this thing is happening? Is it for the CanExecuteChanged
event handler?