Ninject构造函数注入在WPF [英] Ninject constructor injection in WPF

查看:236
本文介绍了Ninject构造函数注入在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以这样的方式,其结果会是这样的注射,我可以在MVC获得使用ninject依赖注入。具体地说就是,如果我使用MVC ninject适配器我可以宣布我的网络控制器,有那么这将自动ninject注入构造函数的参数。

Is it possible to use ninject for dependency injection in such a way that the result would be something like the injection I can get in MVC. To elaborate, if I use the MVC ninject adapter I can declare my web controllers as having constructor parameters which would then automatically be injected by ninject.

不过,我没有发现WPF这样的ninject扩展,这将使我有一个窗口,像这样的:

However, I haven't found such a ninject extension for WPF, which would enable me to have a window such as this:

public partial class MainWindow : Window
{
    private readonly IService injectedService;
    public MainWindow(IService injectedService) 
    {
        this.injectedService = injectedService;
    }
}

我想这样做没有明确使用的iKernel 在我的主要应用程序启动时获得主窗口的实例。我更喜欢使用XAML配置的正常方法来获取主窗口以及所有后续窗口的一个实例。

I would like to do this without explicitly using the IKernel in my main application startup to obtain an instance of mainwindow. I'd much prefer to use the normal method of xaml configuration to obtain an instance of the main window and all subsequent windows.

这可能吗?有什么办法挂接到通过XAML来修改它使用Ninject的构造函数依赖注入所产生的对象创建。

Is this possible? Is there any way to hook into the object creation generated by xaml to modify it to use Ninject for constructor dependency injection.

推荐答案

根据上的评论和放大器;你的困惑,它看起来像MVVM是一个很好的匹配你。目前的挑战是,学习MVVM。

Based on the comments & your confusion, it looks like MVVM is a good match for you. The challenge is, LEARNING MVVM.

所以裂开一个良好的链接并获得滚动。 MVVM是非常容易的事,而且它很容易与Ninject来包装这一切,并把弓就可以了。

So crack open a good link and get rolling. MVVM is surprisingly easy to do, and it's pretty easy to wrap it all up with Ninject and put a bow on it.

最初的学习曲线,如果你不使用为Ninject + MVVM第三方库像我一样,是有点陡峭。因此,这里的一对夫妇的事情,我必须明白:

The initial learning curve if you DON'T use a 3rd party library for Ninject + MVVM like I did, is a bit steep. So here's a couple of things I had to understand:

        DataContext="{Binding Path=ResultViewModel,Source={StaticResource ServiceLocator}}"

这一点除了使得可以触发ninject从你的XAML得到您的视图模型的信息:

This little addition makes allows you to trigger ninject to get your viewmodel information from your XAML:

<Application.Resources>
    <ioc:NinjectServiceLocator x:Key="ServiceLocator" />
</Application.Resources>



这个小动作可以让你的静态资源从App.xaml文件分配给相关类

this little trick allows you to assign that staticresource from your app.xaml file to the relevant class

public class NinjectServiceLocator
{
    private readonly IKernel kernel;

    public NinjectServiceLocator()
    {
        kernel = new StandardKernel(new MyMvvmModule());
    }

    public ResultViewModel ResultViewModel
    {
        get { return kernel.Get<ResultViewModel>(); }
    }
}

这是值得注意的。每个视图模型必须按顺序列出的一个的ServiceLocator属性Ninject生成它们。最后,MyMvvmModule在上面的例子是,你坚持你的倍率load()和绑定所有的接口标准Ninject类。

This is notable. Every viewmodel must be listed as a property in the ServiceLocator in order for Ninject to generate them. Finally, MyMvvmModule in the example above is the standard Ninject class where you stick your override for Load() and bind all your interfaces.

这篇关于Ninject构造函数注入在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆