c# - Window dependency injection -


i want use unity dependency injection in wpf application. window throw system.windows.markup.xamlparseexception:

"for type mainwindow found no default constructor".

this code:

app.xaml.cs:

iunitycontainer container = new unitycontainer(); container.registertype<mainwindow>(); container.registertype<iservice, myservice>(); container.registertype<irepository, myrepository>(); container.resolve<mainwindow>().show(); 

mainwindow.xaml.cs:

public mainwindow(iservice service) {      initializecomponent();      service.test(); } 

you need have default constructor view in order wpf application work properly.

public mainwindow() {      initializecomponent(); } 

and define parameterized constructor :

public mainwindow(iservice service) : this() {      service.test(); } 

Comments