简体   繁体   English

WPF代码后面的Ninject属性注入

[英]Ninject Property Injection in WPF Code Behind

I have a WPF application with Ninject as the DI container. 我有一个WPF应用程序,其中Ninject作为DI容器。 I have added the below code in the App.xaml.cs: 我在App.xaml.cs中添加了以下代码:

public partial class App : Application
{
    private IKernel container;

    protected override void OnStartup(StartupEventArgs e)
    {
        ConfigureContainer();
        base.OnStartup(e);
    }

    private void ConfigureContainer()
    {
        this.container = new StandardKernel();
        this.container.Bind<IAccountTypeRepository>().To<AccountTypeRepository>();
        this.container.Bind<IMainWindow>().To<MainWindow>();
        var mainWindow = this.container.Get<IMainWindow>();
        mainWindow.Show();
    }
}

When I try to call the IAccountTypeRepository in the code behind of my UserControl it throws a null exception. 当我尝试在UserControl后面的代码中调用IAccountTypeRepository时,将引发null异常。 Can you please help what is missing in my code. 您能帮忙我代码中缺少的内容吗? Thank you so much. 非常感谢。 I need to have it as a Property Injection. 我需要将其作为属性注入。

public interface IAccountTypeRepository
{
    void Update(SqlParameter[] param);

    DataTable GetAll(SqlParameter[] param);
}



public class AccountTypeRepository : IAccountTypeRepository
{
    public void Update(SqlParameter[] param)
    {
        var sqlRepository = new SqlRepository();
        sqlRepository.ExecuteNonQuery("MyStoredProc", param);
    }


    public DataTable GetAll(SqlParameter[] param)
    {
        var sqlRepository = new SqlRepository();

        return sqlRepository.ExecuteDataTable("MyStoredProc", param);
    }
}

Error Encountered: 遇到错误:

在此处输入图片说明

When the XAML parser creates an object it doesn't inform the DI container (ninject) that it's doing so. XAML解析器创建对象时,不会通知DI容器(ninject)正在这样做。 So ninject cannot work it's magic. 因此,ninject无法正常工作,这是不可思议的。 That means you'll have to manually call kernel.Inject(myUserControl) for myUserControls properties to be injected. 这意味着您必须手动调用kernel.Inject(myUserControl)才能注入myUserControls属性。

Also

The object only exists once the constructor is done . 该对象仅在构造函数完成后才存在。 Therefore, property injection happens after the constructor was executed. 因此,属性注入发生在构造函数执行之后。 So there's no way that you can access a property-injected value from a constructor. 因此,您无法从构造函数访问属性注入的值。

If you want some logic to be executed after instantiation, what you can do is as follows: 如果要在实例化后执行某些逻辑,可以执行以下操作:

public partial class Window
{
...
    public void Initialize()
    {
    ... that's where the logic goes...
    }
...
}

Bind<IWindow>().To<Window>()
    .OnActivation(x => x.Initialize());

The delegate passed to OnActivation will be executed once the object was created. 创建对象后,将执行传递给OnActivation的委托。 It will be called exactly once per object. 每个对象将只调用一次。 So if you have an InSingletonScope() binding and inject the object in multiple places, the activation method will still only be called once. 因此,如果您具有InSingletonScope()绑定并将对象注入多个位置,则激活方法仍将仅被调用一次。

However when the object is instantiated by the XAML parser and you call kernel.Inject(...) on it, this won't work either. 但是,当对象由XAML解析器实例化并在其上调用kernel.Inject(...)时,该方法也不起作用。 But since you already call kernel.Inject(...) you can just as well call foo.Initialize() yourself. 但是由于您已经调用kernel.Inject(...)您也可以自己调用foo.Initialize()

Instantiating Views by DI container 通过DI容器实例化视图

Some frameworks, like Caliburn.Micro, allow you to instantiate some Views - under certain circumstances - through the DI container. 一些框架,例如Caliburn.Micro,允许您在特定情况下通过DI容器实例化某些视图。 However this means that these controls won't show up in the designer. 但是,这意味着这些控件不会显示在设计器中。 They'll just be blank. 他们只是空白。 For example see this answer 例如看这个答案

Use NinjectModule and your problem will be solved. 使用NinjectModule,您的问题将得到解决。

please have a look following link. 请查看以下链接。

https://github.com/ninject/Ninject/wiki/Modules-and-the-Kernel https://github.com/ninject/Ninject/wiki/Modules-and-the-Kernel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM