C# 九项表格澄清
我有一个C# 九项表格澄清,c#,visual-studio-2010,ninject,C#,Visual Studio 2010,Ninject,我有一个ModuleLoader:NinjectModule,我在这里绑定所有内容 首先我使用 Bind<Form>().To<Main>(); 其中,\u mainKernel是一个ninject标准内核 然后我使用Application.Run(form) 这是正确的吗 当涉及到Windows.Forms时,我不确定该绑定什么 谢谢您的帮助。您不应该真正绑定到System.Windows.Forms.Form。Ninject主要用于将接口绑定到具体类型,以便您可以将
ModuleLoader:NinjectModule
,我在这里绑定所有内容
首先我使用
Bind<Form>().To<Main>();
其中,\u mainKernel
是一个ninject标准内核
然后我使用Application.Run(form)
这是正确的吗
当涉及到Windows.Forms
时,我不确定该绑定什么
谢谢您的帮助。您不应该真正绑定到
System.Windows.Forms.Form
。Ninject主要用于将接口绑定到具体类型,以便您可以将依赖项作为接口传递,并在运行时/测试期间切换出具体实现
如果您只想使用Ninject以这种方式创建表单,只需使用Bind().ToSelf()
然后执行kernel.Get()
。但是,如果您直接请求具体类型,并且它不需要任何依赖项,那么使用Ninject初始化它没有多大意义
在您的情况下,如果表单实现了一个接口,那么您将执行以下操作:Bind().To()
,并从Ninject请求接口类型。通常,您的界面不应该绑定到“表单”的概念,但它应该与实现无关(因此,稍后您可以生成CLI和网站版本,只需交换Ninject绑定)
您可以使用模型视图演示者设计模式(或变体)来实现以下目标:
public interface IUserView
{
string FirstName { get; }
string LastName { get; }
}
public class UserForm : IUserView, Form
{
//initialise all your Form controls here
public string FirstName
{
get { return this.txtFirstName.Text; }
}
public string LastName
{
get { return this.txtLastName.Text; }
}
}
public class UserController
{
private readonly IUserView view;
public UserController(IUserView view)
{
this.view = view;
}
public void DoSomething()
{
Console.WriteLine("{0} {1}", view.FirstName, view.LastName);
}
}
Bind<IUserView>().To<UserForm>();
Bind<UserController>().ToSelf();
//will inject a UserForm automatically, in the MVP pattern the view would inject itself though
UserController uc = kernel.Get<UserController>();
uc.DoSomething();
公共接口IUserView
{
字符串名{get;}
字符串LastName{get;}
}
公共类用户窗体:IUserView,窗体
{
//在此处初始化所有表单控件
公共字符串名
{
获取{返回this.txtFirstName.Text;}
}
公共字符串姓氏
{
获取{返回this.txtLastName.Text;}
}
}
公共类用户控制器
{
私有只读IUserView视图;
公共用户控制器(IUserView视图)
{
this.view=视图;
}
公共无效剂量测定法()
{
Console.WriteLine(“{0}{1}”,view.FirstName,view.LastName);
}
}
绑定()到();
Bind().ToSelf();
//将自动注入一个用户表单,但在MVP模式中视图将注入自身
UserController uc=kernel.Get();
uc.DoSomething();
您实际上没有提出任何问题。确认书有用吗?
public interface IUserView
{
string FirstName { get; }
string LastName { get; }
}
public class UserForm : IUserView, Form
{
//initialise all your Form controls here
public string FirstName
{
get { return this.txtFirstName.Text; }
}
public string LastName
{
get { return this.txtLastName.Text; }
}
}
public class UserController
{
private readonly IUserView view;
public UserController(IUserView view)
{
this.view = view;
}
public void DoSomething()
{
Console.WriteLine("{0} {1}", view.FirstName, view.LastName);
}
}
Bind<IUserView>().To<UserForm>();
Bind<UserController>().ToSelf();
//will inject a UserForm automatically, in the MVP pattern the view would inject itself though
UserController uc = kernel.Get<UserController>();
uc.DoSomething();