【发布时间】:2018-05-24 23:09:11
【问题描述】:
我将 Caliburn.Micro 与 Xamarin.Forms 结合使用。在我的 App 类中,我通过 SimpleContainer 的 PerRequest 方法向类 LicenseInfoImplementation 注册了一个接口 ILicenseInfo。 然后,CM 在创建我的视图模型时注入一个对象(请参阅 ViewModelOne),这正是我想要的。但是,我不知道如何将其扩展到对象集合。假设我希望 CM 实例化 ViewModelTwo ,它需要一个对象集合。我将如何更改 App.cs 和 ViewModelTwo 的 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
}
【问题讨论】:
-
写一个包装器怎么样,所以你会写一个接口
ILicenseInfoContainer与属性 IEnumerable做你的实现,在你的 CM 容器中注册并在你的 viewModel 中使用它 -
这将是一个可行的解决方法。另一种方法是创建一个默认构造函数,并在其中通过 IEnumberable
impl = IoC.Get (); 检索所需的集合;理想情况下,我正在寻找一种解决方案,我不必更改视图模型方法的签名或创建显式 CM 依赖项(例如在视图模型中使用 IoC 类)
标签: xamarin.forms caliburn.micro