Caliburn.Micro 正在使用 Xamarin.Forms 应用程序注入集合

Caliburn.Micro injecting a Collection with Xamarin.Forms Application

我正在结合使用 Caliburn.MicroXamarin.Forms。在我的应用程序 class 中,我通过 SimpleContainer 的 PerRequest 方法使用 class LicenseInfoImplementation 注册了接口 ILicenseInfo。 然后,CM 在创建我的视图模型时注入一个对象(请参阅 ViewModelOne),这正是我想要的。但是,我不知道如何将其扩展到对象集合。假设我希望 CM 实例化需要对象集合的 ViewModelTwo。我将如何更改 ViewModelTwo 的 App.cs 和 XAML 才能实现这一点?

public partial class App : FormsApplication
{
    private readonly SimpleContainer _container;

    public App (SimpleContainer container)
    {
        InitializeComponent();
        this._container = container;    
        // register interface and class
        _container.PerRequest<ILicenseInfo, LicenseInfoImplementation>();   
        //....
    }
}

public ViewModelOne(ILicenseInfo license)
{
    // Great, Caliburn.Micro injects an LicenseInfoImplementation object
}

public ViewModelTwo(IEnumerable<ILicenseInfo> licenses)
{
    // No idea 
}

我终于使用了下面的模式。不是最优雅的方式,而是我发现的最好的方式...

public ViewModelTwo() : base (IoC.GetAll<ILicenseInfo>())
{
}

public ViewModelTwo(IEnumerable<ILicenseInfo> licenses)
{
// Do something with licenses
}