I am creating a factory using interface as
public interface ICommandFactory
{
ICommand CreateCommand(Action executeMethod);
ICommand CreateCommand(Action executeMethod, Func<bool> canExecuteMethod);
}
public class DelegateCommand : DelegateCommand<object>
{
public DelegateCommand(Action executeMethod)
: base(o => executeMethod())
{
}
public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)
: base(o => executeMethod(), o => canExecuteMethod())
{
}
}
public class DelegateCommand<T> : ICommand
{
public DelegateCommand(Action<T> executeMethod)
: this(executeMethod, null)
{
}
public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
{
}
}
My Ninject binding is done using
_kernel.Bind(x => x.FromAssembliesMatching("xyz*")
.SelectAllClasses()
.BindAllInterfaces());
_kernel.Bind(x => x.FromAssembliesMatching("xyz*")
.SelectAllInterfaces()
.EndingWith("Factory")
.BindToFactory()
.Configure(c => c.InSingletonScope()));
When I call in my view model it is causing activation exception although I tried with Named binding.
public class MyViewModel
{
public ICommand SaveCommand {get; private set;}
public MyViewModel(ICommandFactory commandFactory)
{
SaveCommand = commandFactory.CreateCommand(Save, () => SelectedTask != null);
}
}
The exception is caused in DelegateCommand's constructor on 'o => canExecuteMethod()' line. Also, I cannot use constructor param passing in Ninject as my logic for canExecute is in my ViewModel. Any solution or fix is accepted.