c# - IoC container in MVVMLight - How to pass concrete implementation to the specified element? -


I am currently facing the following problem with the IoC container from MVVM light - I have the interface IDataService , which is being implemented by two classes: DataService1 and DataService2 . I also have the main view model category, which has several dependencies, with one IDataService .

The issue is, I do not have MainViewModel with DataService2 , another one, how can I do it? I have tried both "instances" of IDataService and MainViewModel , such as:

  class MainViewModel {// MainViewModel has many dependencies, Public Main Week Model (Xx, YY, Z, MM, NN, IDTA Service Service Services) {}} (...) Simple IOC Default. Registrar & lt; IDataService & gt; (() => New Data S Service 1 (), "A"); SimpleIoc.Default.Register & lt; IDataService & gt; (() => New DataService2 (), "B"); (...) var viewmodel = Simple Ioc.Default.GetInstance & lt; MainViewModel & gt; ("B"); // runtime exception  

but I get a runtime exception that basically says that main view model can not be solved - which I have understood if I I want to solve it Closed Main View Model ("B"), all its dependencies must be closed. This is not what I want - I want to solve MainViewModel , in which there is a default dependency, apart from one.

How can I do this? Thanks in advance for any help I'm clearly missing something.

Hate to say, but it looks the only way. You must register a keyed factories:

  SimpleIoc.Default.Register & lt; MainViewModel & gt; New MainViewModel (New X), New Y (), New Z (), New M (), New N (), SimpleIak Default.Gate Instance & lt; IDataService & gt; (" B "))," B ");  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -