Ninject: 实现 WithConstructorArgument(字符串名称, Func回调)
我有一个 MVVM WP7 应用程序,其中我尝试将一个值从一个 Page/ViewModel 发送到第二个 ViewModel 的构造函数。我已经设置了 Ninject,并使用以下行使其与静态测试值一起使用:
this.Bind<TaskViewModel>().ToSelf().WithConstructorArgument("TaskID", 2690)
同样,它与静态值一起使用,但我需要它是用户选择的变量。我被告知要使用重载,
WithConstructorArgument(string name, Func<IContext,object> callback).
我认为这个回调会调用第一个 ViewModel 上的函数并获取值。
但我并没有成功,我承认我在 Ninject 或使用 Func 回调参数方面都不是很有经验。我尝试设置委托和函数来从第一个 ViewModel 获取值,但这给出了一个错误,表示我正在尝试传递类型。我到底如何指定该参数来使用回调,以及在第一个 ViewModel 或其他内容中使用委托是否正确?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在您的另一篇文章中已经说过的那样,在 get 上传递参数可能是更好的方法。因此,在您的引导程序中创建一个工厂接口
(负责使用 Ninject 创建所有内容的程序集,它通常应该是您实现具有业务价值的所有内容的地方的另一个程序集)添加实现,
然后将工厂注入到您的任务选择命令中,并在以下情况下调用工厂:任务已选择。
As I already said in your other post passing the argument on get is probably the better way. Therefor create a factory interface
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
Then inject the factory to your task selection command and call the factory whena task is selected.