没有匹配的绑定可用,并且在使用 Lazy 时,该类型不可自绑定

本文关键字:绑定 类型 Lazy | 更新日期: 2023-09-27 18:34:37

提前感谢那些花时间阅读本文的人!

我正在尝试使用 Ninject 解析一对名为 CategoryListBox 和 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();
}

我有以下"基本"类实现接口:

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 个"具体"表单来实现 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");

我按如下方式填充类别列表框数据源:

        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();
    }

我的目标是在单击子类别列表框之前不实例化子窗体。

相当确定我的做法是错误的,欢迎任何建议。

没有匹配的绑定可用,并且在使用 Lazy<T> 时,该类型不可自绑定

我能够通过将ninject.extensions.contextpreservation 添加到我的参考文献来解决我的问题