2014-12-19 16 views
1

在此先感謝您花時間閱讀本文的人!沒有匹配的綁定可用,並且類型在使用Lazy時不可自行綁定。<T>

我正在嘗試使用Ninject來解析名爲CategoryListBox和SubCategoryListBox的一對級聯列表框中包含的項,然後Lazy在單擊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 {} 

我通過NuG引用了Ninject和Ninject.Extensions.Factory等我usings看起來像這樣

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當您雙擊該項目我試圖延遲加載子窗體,這是當我碰上了「無匹配的綁定是用」錯誤

private void SubCategoryListBox_MouseDoubleClick(object sender, MouseEventArgs e) 
    { 
     ISubCategory selected = (ISubCategory)((ListBox)sender).SelectedItem; 
     selected.SubForm.Value.Show(); 
    } 

我的目標是不實例化SubForms直到我點擊SubCategoryListBox。

我相當肯定我會以錯誤的方式去解決問題,並歡迎任何建議。

回答

0

我能夠加入ninject.extensions.contextpreservation我引用我解決問題

相關問題