1

我正在使用 caliburn.micro 并且我已经配置了 Bootstrapper ,如下所示:bootstrapper configuration,但不幸的是我找不到将导出值请求为新实例的方法 - 容器总是返回单例。

我也一直在使用 PhoneContainer (WindowsPhone),它具有 PerRequest 和 Singleton 等强大功能。WPF有类似的东西吗?

4

1 回答 1

2

If you are using MEF as in the article then when ever you want an instance to be created Per Request export it like this:

interface IFileReader {
    int ReadChar(string fileName);
}

[Export(typeof(IFileReader))]
[PartCreationPolicy(CreationPolicy.NonShared)]
class FileReaderImpl : IFileReader {
    // IMPLEMENTATION GOES HERE
}

Of course this is specific to MEF (MEF Lifestyles), but if you want you can use your favorite container, there is one provided with Caliburn.Micro called SimpleContainer, the docs for that will be out soon but the API is pretty intuitive and if you like you can use other containers like Unity, Ninject, etc. Check out this link about using Unity, the process for other containers is pretty similar.

于 2013-06-02T21:36:20.507 回答