Pankaj
Pankaj

Reputation: 10105

Ninject implementation in Window Forms - C#

I am using Window Forms C# with Ninject version: 2.0.0.1

First Installed the Ninject Package : ninject.extensions.infrastructure.winforms

Second I created the new class with following code.

public class CustomModule : NinjectModule
{
    public override void Load()
    {
        Bind<IDAL.IORDR>().To<DAL.DAL_ORDR>();
    }
}

Third in the window form I did below

public partial class SODetails : Form
{
    public IORDR _IORDR { get; set; }

    [Inject]
    public SODetails(IORDR ORDR)
    {
        _IORDR = ORDR;
    }

    public SODetails()
    {
        InitializeComponent();
    }
}

Finally in the Program.cs, I did the below code

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    var kernel = new StandardKernel(new CustomModule());
    var form = kernel.Get<SODetails>();
    Application.Run(form);
}

Although I am able to Inject the dependencies successfully but the form is not showing any control. Am I doing the Ninject implementation in wrong way ?

Upvotes: 0

Views: 1699

Answers (2)

Pankaj
Pankaj

Reputation: 10105

I changed the following code from

public partial class SODetails : Form
{
    public IORDR _IORDR { get; set; }

    [Inject]
    public SODetails(IORDR ORDR)
    {
        _IORDR = ORDR;
    }

    public SODetails()
    {
        InitializeComponent();
    }
}

to

public partial class SODetails : Form
{
    public IORDR _IORDR { get; set; }

    [Inject]
    public SODetails(IORDR ORDR)
    {
        _IORDR = ORDR;
        InitializeComponent();
    }

    public SODetails()
    {

InitializeComponent();

    }
}

Upvotes: 1

Kapoor
Kapoor

Reputation: 1428

I see the problem.

So your SODetails form is up and running but it doesnt show any controls on it that you have added in designer . I'm I correct ?

The issue is the InitializeComponent() is not called.

Please change the code, so as to call the default constructor & all will work fine. I made a small change to your code for constructor chaining public SODetails(IORDR ORDR) : this() {....}

public IORDR _IORDR { get; set; }

    [Inject]
    public SODetails(IORDR ORDR) : this()
    {
        _IORDR = ORDR;
    }

    public SODetails()
    {
        InitializeComponent();
    }


    public IORDR _IORDR { get; set; }

    [Inject]
    public SODetails(IORDR ORDR) : this()
    {
        _IORDR = ORDR;
    }

    public SODetails()
    {
        InitializeComponent();
    }

Upvotes: 0

Related Questions