I have a MVVM WP7 app in which I'm trying to send a value from one Page/ViewModel to the contructor of a second ViewModel. I already have Ninject set up and got this to work with a static test value using a line such as:
this.Bind<TaskViewModel>().ToSelf().WithConstructorArgument("TaskID", 2690)
Again, that works with a static value but I need it to be a variable selected by the user. I've been told to use the overload
WithConstructorArgument(string name, Func<IContext,object> callback).
I would think that this callback would call a function on the first ViewModel and get the value.
But I have not been successful, readily admitting I am not very experienced in either Ninject or using a Func callback argument. I've tried setting up a delegate and function to get the value from the first ViewModel but that gives an error saying I'm trying to pass in a type. How exactly do I specify that argument to use a callback and am I correct to use a delegate in the first ViewModel or something else?
As I already said in your other post passing the argument on get is probably the better way. Therefor create a factory interface
public interface ITaskViewFactory
{
TaskViewModel CreateTaskViewModel(int id);
}
In your bootstrapper (The assembly responsible to create everything using Ninject, which should normally be another one than where you implement everything with business value) add the implementation
Public class TaskViewFactory : ITaskViewFactory
{
Private IKernel kernel;
Public TaskViewFactory(IKernel kernel)
{
this.kernel = kernel;
}
public TaskViewModel CreateTaskViewModel(int taskId)
{
this.kernel.Get<ITaskViewModel>(new ConstructorArgument("TaskId", taskId);
}
}
Then inject the factory to your task selection command and call the factory whena task is selected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With