Minimal Example:
Install the package by Nuget or Package Manager Console
Install-Package Ninject
Create a class that is responsible for solving dependencies:
FormModule
using Ninject.Modules;
public class FormModule : NinjectModule
{
public override void Load()
{
Bind<IMount>().To<Mount>();
}
public static FormModule Create()
{
return new FormModule();
}
}
in the method Load
call the Bind
for class configuration and put all classes that will be used in your project and that are dependencies of other forms.
Create a class that will be responsible for instantiating Form
and consequently the their dependencies:
FormResolve
using Ninject;
using Ninject.Modules;
public class FormResolve
{
private static IKernel _ninjectKernel;
public static void Wire(INinjectModule module)
{
_ninjectKernel = new StandardKernel(module);
}
public static T Resolve<T>()
{
return _ninjectKernel.Get<T>();
}
}
In the archive Program.cs
configure as follows:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormResolve.Wire(FormModule.Create());
Application.Run(FormResolve.Resolve<Form1>());
}
}
}
With these two classes and the configuration done in the Program.cs
is already ready to solve dependencies.
Example: Form1
using Ninject;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private IMount Mount { get; set; }
[Inject()]
public Form1(IMount mount)
{
InitializeComponent();
Mount = mount;
}
private void Form1_Load(object sender, System.EventArgs e)
{
label1.Text = Mount?.GetDateTime().ToLongDateString();
}
private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm = FormResolve.Resolve<Form2>();
frm.ShowDialog();
}
}
}
To call the form2
or any form
follow the example of the button below where form1
calls the form2
:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm = FormResolve.Resolve<Form2>();
frm.ShowDialog();
}
References:
Thanks Virgilio.... The Program.Cs class is giving error (Underlining red yet....) ;
– Master JR
Depending on the version you installed Ninject, red should be
obsoleto
, please check this out and if this is it install the stable Ninject which is the versionInstall-Package Ninject -Version 3.2.2
– novic
Yes, the latest version of Ninject has been installed... The problem is that in Program.Cs it is as if the Application is no longer available or without reference...
– Master JR
Look, I saw your project, it’s missing from it
class
I ended up remembering so I made a minimal example as it should be, in your project it was impossible for me to keep coding that was why I decided to contribute an answer this way, being an ideal project for adaptation with yours. I mean, the mistake is in your project and not in my proposal.– novic
OK Virgilio. Anyway, thanks anyway!
– Master JR
Look @Jalberromano excuse something, but, your attached project in question missing
class
and that kind of breaks the rule of here we propose solution, even this is functional beyond is that the project in question lackclass
!– novic