I tried a lot of approches, but no luck. How can I resolve the IWindowService?
This is what I have:
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")));
The implementation of the window service:
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;
}
Error: The type WindowService does not have a constructor that takes the parameters (IWindow).
Here's a working example. In the scenario you have provided there's no reason for the named registrations, so I removed them. But I'm sure you can get it to work with them if you really want.
The key is to register IWindowService
with:
container.RegisterType(typeof (IWindowService<>), typeof (WindowService<>))
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Registering dependencies ...");
var container = new UnityContainer();
container.RegisterType(typeof (IWindowService<>), typeof (WindowService<>));
container.RegisterType<IWindow, Window>();
container.RegisterType<IvmMain, ViewModel>(); // Or possibly RegisterInstance ?
var foo = container.Resolve<IWindowService<IvmMain>>();
Console.ReadKey();
}
}
public interface IWindowService<T>
{
}
public interface IWindow
{
}
public interface IvmMain
{
}
public class ViewModel : IvmMain
{
}
public class Window : IWindow
{
}
public class WindowService<TViewModel> : IWindowService<TViewModel>
{
public WindowService(IWindow win, TViewModel vm)
{
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments