I have a UserControl
which is bound to a viewmodel
. The viewmodel
has parameters in the constructor which is done via dependency injection. The dependency injection is working fine up to this point.
And then I have a CustomUserControl
which is Used in my UserControl1View
.
How do I get the Dependency injection working in my CustomUserControl
?
I am new to dependency injection and did some research, but can't seem to get it working. I get an error on
public partial class UserControl1View : UserControl, IView {
public UserControl1View( ) {
InitializeComponent( );
}
}
This is the error:
Here is the example of the code.
UserControl:
public partial class UserControl1View : UserControl, IView {
public UserControl1View( ) {
InitializeComponent( );
}
}
UserControlViewModel:
public class UserControl1ViewModel
{
private readonly ISomeDataService dataService;
public UserControl1ViewModel (ISomeDataService dataservice, IUnityContainer container )
{
//Please note that the Dependency injection still works in this class, to much to explain the whole structure.
this.dataService = dataservice;
container.RegisterType( typeof( IView ), typeof( CustomUserControlView ) );
var view = container.Resolve<CustomUserControlView>( );
}
XAML:
<uc:CustomUserControlView/>
CustomUserControl:
public partial class CustomUserControlView : UserControl, IView
{
public CustomUserControlView(IUnityContainer container)
{
container.RegisterType( typeof( IViewModel ), typeof( CustomControlViewModel ) );
var viewModel = container.Resolve<CustomControlViewModel>( );
this.DataContext = viewModel;
InitializeComponent( );
}
}
CustomUserControlViewModel:
public partial class CustomUserControlViewModel : UserControl, IView
{
private readonly ISomeDataService dataService;
public CustomUserControlViewModel(ISomeDataService dataservice)
{
var data = dataService.GetsomeCollection()
}
}