How to pass Dispatcher to a class through constructor by Ninject

41 views Asked by At

I have a class which takes a parameter of type Dispatcher:

 public class MessagesViewModel
{
private readonly DispatcherTimer keepAliveTimer;     
    public MessagesViewModel(Dispatcher dispatcher)
         {
             keepAliveTimer = new DispatcherTimer(TimeSpan.FromSeconds(30), DispatcherPriority.Normal, KeepAlive, dispatcher);
      
        
         }
}

How can I use Ninject to configure this class? I have already using this code:

kernel.Bind<MessagesViewModel>().ToSelf().WithConstructorArgument( new ConstructorArgument("dispatcher", Dispatcher.CurrentDispatcher));

but it throws this exception:

Error activating Dispatcher using implicit self-binding of Dispatcher
No constructor was available to create an instance of the implementation type.

Activation path:
  3) Injection of dependency Dispatcher into parameter dispatcher of constructor of type MessagesViewModel
  2) Injection of dependency MessagesViewModel into parameter messagesViewModel of constructor of type QCApplicationController
  1) Request for QCApplicationController

Suggestions:
  1) Ensure that the implementation type has a public constructor.
  2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.
0

There are 0 answers