How to register an instance of a class, including a parameter in SimpleIOC

I need to create an instance of ViewModel with a specific parameter passed to the ViewModel when it is created. At the same time, this ViewModel instance must be registered with SimpleIOC

I thought this was a method for him:

SimpleIoc.Register<TClass> Method (Func<TClass>, String, Boolean) 

with true for the last parameter for instant creation. therefore, if I understood correctly, this method wants a reference to the method that will create my ViewModel instance.

This is called ClassFactory, as it seems.

I tried to do it myself, but all I get is

 cannot convert from <Class> to System.Func<Class> 

so it seems that im always passes an instance of the class, not the method that should create it.

can someone give a short example of how i can make this work

 public class ClassFactory { public ChatWindow CreateChatWindow(RosterItemX ri) { return new ChatWindow(ri); } } public class ViewModelLocator { . . . . public static void CreateWindow(RosterItemX riv) { ClassFactory cf = new ClassFactory; SimpleIoc.Default.Register<ChatWindow>(cf.CreateChatWindow(ri), "key", true ) var _messageWindow = new MessageWindow(); _messageWindow.Show(); } } class ChatMessage { RosterItemX ri = new RosterItemX(); ViewModelLocator.CreateWindow(ri); } 
0
c # ioc-container mvvm-light
Feb 23 '16 at 19:44
source share
1 answer

As you said yourself, you are giving an instance of the ChatWindow function to the function. However, he does expect the function that creates ChatWindow . Just convert the first parameter to lambda with () =>

SimpleIoc.Default.Register<ChatWindow>(() => cf.CreateChatWindow(ri), "key", true);

+3
Feb 23 '16 at 21:00
source share



All Articles