C# 没有匹配的绑定可用,并且使用Lazy时该类型不可自绑定<;T>;
提前感谢大家抽出时间来阅读本文 我试图使用Ninject解析一对名为CategoryListBox和SubCategoryListBox的级联列表框中包含的项,然后在单击SubCategoryListBox项时延迟加载表单 我有以下接口:C# 没有匹配的绑定可用,并且使用Lazy时该类型不可自绑定<;T>;,c#,ninject,lazy-loading,C#,Ninject,Lazy Loading,提前感谢大家抽出时间来阅读本文 我试图使用Ninject解析一对名为CategoryListBox和SubCategoryListBox的级联列表框中包含的项,然后在单击SubCategoryListBox项时延迟加载表单 我有以下接口: public interface ICategory { string Caption { get; set; } ISubCategory[] SubCategories { get; set; } } public interface I
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();
}
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;
}
}
我通过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");
当您双击子类别列表框中的项目时,我尝试延迟加载子窗体,也就是当我遇到“没有可用的匹配绑定”错误时
我的目标是在单击子类别列表框之前不实例化子窗体
我敢肯定我做得不对,欢迎提出任何建议。我可以通过在我的参考资料中添加ninject.extensions.contextpreservation来解决我的问题
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");
List<ICategory> categories = kernel.GetAll<ICategory>().ToList<ICategory>();
CategoryListBox.DataSource = categories;
CategoryListBox.DisplayMember = "Caption";
private void CategoryListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
ICategory selected = (ICategory)((ListBox)sender).SelectedItem;
SubCategoryListBox.DataSource = selected.SubCategories;
SubCategoryListBox.DisplayMember = "Caption";
}
private void SubCategoryListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
ISubCategory selected = (ISubCategory)((ListBox)sender).SelectedItem;
selected.SubForm.Value.Show();
}