1
我尝试了很多方法,但没有运气。 如何解决IWindowService?如何使用一个通用构造函数参数注册/解析一个类型
这是我有:
var c = new UnityContainer();
c.RegisterType<IvmMain, vmMain>("mvm");
c.RegisterType<IWindow, MainWindow>("mw");
c.RegisterType(typeof(IWindowService<IvmMain>), typeof(WindowService<vmMain>),"win",
new InjectionConstructor(new ResolvedParameter<IWindow>("mw")),
new InjectionConstructor(new ResolvedParameter<IvmMain>("mvm")));
窗口服务的实现:
public class WindowService<TViewModel> : IWindowService<TViewModel>
{
readonly Window _win;
TViewModel _vm;
public WindowService(IWindow win, TViewModel vm)
{
_vm = vm;
_win = win as Window;
_win.DataContext = vm;
}
错误: 类型WindowService没有一个构造函数的参数(IWindow )。
感谢分享! – Legends
还有一个问题(我可以为此打开一个新的Q): 我使用构造函数注入,所以我不明确使用resolve方法。 如果我有另一个'container.RegisterType();'并且想在另一个类中使用这个作为constrtuctor参数? 然后会是什么方法? 现在我需要命名注册......而不使用'[Dependency]'属性 –
Legends
某种方式您确实需要知道您想要解析的类型。建设者注入与否。有很多方法可以做到这一点。看看这个答案。 http://stackoverflow.com/questions/38475047/breaking-solid-principles-in-multiple-implementation-of-an-interface/38475681#38475681 – smoksnes