提前感谢那些花时间阅读本文的人!
我试图使用Ninject来解析名为CategoryListBox和SubCategoryListBox的一对级联列表框中包含的项目,然后在单击SubCategoryListBox项目时延迟加载表单。
我有以下界面:
public interface ICategory
{
string Caption { get; set; }
ISubCategory[] SubCategories { get; set; }
}
public interface ISubCategory
{
string Caption { get; set; }
Lazy<ISubForm> SubForm { get; set; }
}
public interface ISubForm
{
void Show();
}
我有以下&#34; Base&#34;类实现接口:
public class BaseCategory : ICategory
{
public string Caption { get; set; }
public ISubCategory[] SubCategories { get; set; }
public BaseCategory(string caption, ISubCategory[] subCategories)
{
Caption = caption;
SubCategories = subCategories;
}
}
public class BaseSubCategory : ISubCategory
{
public string Caption { get; set; }
public Lazy<ISubForm> SubForm { get; set; }
public BaseSubCategory(string caption, Lazy<ISubForm> subForm)
{
Caption = caption;
SubForm = subForm;
}
}
我有4&#34;混凝土&#34;用于实现ISubForm接口的表单如下:
public partial class SubForm1A : Form, ISubForm {}
public partial class SubForm1B : Form, ISubForm {}
public partial class SubForm2A : Form, ISubForm {}
public partial class SubForm2B : Form, ISubForm {}
我通过NuGet引用了Ninject和Ninject.Extensions.Factory,我的用法看起来像这样
using Ninject;
using Ninject.Extensions.Factory;
我的绑定语句如下所示:
IKernel kernel = new StandardKernel();
kernel.Bind<ICategory>().To<BaseCategory>().Named("Category 1").WithConstructorArgument("One");
kernel.Bind<ISubCategory>().To<BaseSubCategory>().WhenParentNamed("Category 1").Named("SubCategory 1A").WithConstructorArgument("1A");
kernel.Bind<ISubCategory>().To<BaseSubCategory>().WhenParentNamed("Category 1").Named("SubCategory 1B").WithConstructorArgument("1B");
kernel.Bind<ISubForm>().To<SubForm1A>().WhenParentNamed("SubCategory 1A");
kernel.Bind<ISubForm>().To<SubForm1B>().WhenParentNamed("SubCategory 1B");
kernel.Bind<ICategory>().To<BaseCategory>().Named("Category 2").WithConstructorArgument("Two");
kernel.Bind<ISubCategory>().To<BaseSubCategory>().WhenParentNamed("Category 2").Named("SubCategory 2A").WithConstructorArgument("2A");
kernel.Bind<ISubCategory>().To<BaseSubCategory>().WhenParentNamed("Category 2").Named("SubCategory 2B").WithConstructorArgument("2B");
kernel.Bind<ISubForm>().To<SubForm2A>().WhenParentNamed("SubCategory 2A");
kernel.Bind<ISubForm>().To<SubForm2B>().WhenParentNamed("SubCategory 2B");
我按如下方式填充CategoryListBox数据源:
List<ICategory> categories = kernel.GetAll<ICategory>().ToList<ICategory>();
CategoryListBox.DataSource = categories;
CategoryListBox.DisplayMember = "Caption";
当您双击CategoryListBox中的项目时,它会按如下方式填充SubCategoryListBox:
private void CategoryListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
ICategory selected = (ICategory)((ListBox)sender).SelectedItem;
SubCategoryListBox.DataSource = selected.SubCategories;
SubCategoryListBox.DisplayMember = "Caption";
}
当您双击SubCategoryListBox中的项目时,我尝试延迟加载SubForm,这是当我遇到&#34;没有匹配的绑定可用时#34;错误
private void SubCategoryListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
ISubCategory selected = (ISubCategory)((ListBox)sender).SelectedItem;
selected.SubForm.Value.Show();
}
我的目标是在单击SubCategoryListBox之前不实例化SubForms。
我很确定我会采用错误的方式进行,欢迎提出任何建议。
答案 0 :(得分:0)
我能够通过将 ninject.extensions.contextpreservation 添加到我的参考资料来解决我的问题