How can I inject my CustomerRepository into my CustomerVM view model? In my view, a WPF Page I have:
<Page.DataContext> <viewModel:CustomerVM/> </Page.DataContext>
But my view model constructor obviously has parameter passed in,
public CustomerVM(ICustomerRepository customerRepository) { //this._customerRepository = customerRepository; }
I get
Type ‘CustomerVM’ is not usable as an object element because it is not
public or does not define a public parameterless constructor or a type
converter.
Really struggling to heck.
Any help appreciated.
I don’t think you can initialize the DataContext within the XAML if you’re using dependency injection. Set the DataContext in the code-behind for the view so Unity can resolve the dependencies. Try adding this to YourView.xaml.cs:
The above will work if you are resolving your views through Unity. If not, you can also use the ServiceLocator to resolve the view model:
You might also need to add the following somewhere in your registration code to setup the ServiceLocator if you’re not using Prism: