title: Simple Injector and MVVM Null object reference
body: <p>First off, new with MVVM and WPF, building up my first application following MVVM. I tried to find answers on the internet but without any results, so decided to ask here.</p>
  
  <p>I have the following constructor in my MainWindow:</p>
  
  <pre><code>container = new SimpleInjector.Container();
  
  container.Register&lt;IContext, ApplicationContext&gt;(Lifestyle.Singleton);
              container.Register&lt;SearchOrderViewModel&gt;();
  
  _searchOrderViewModel = container.GetInstance&lt;SearchOrderViewModel&gt;();
  
  NavigationCommand = new RelayCommand&lt;string&gt;(OnNavigation);
  </code></pre>
  
  <p>And then I use a button with Command to set the Content to ContentControl in XML with this:</p>
  
  <pre><code>private void OnNavigation(string destination)
          {
              switch (destination)
              {
                  case "orderSearch":
                      CurrentViewModel = _searchOrderViewModel;
                      break;
                  default:
                      break;
              }
          }
  </code></pre>
  
  <p>And finally my SearchOrderViewModel constructor</p>
  
  <pre><code>private IContext InstanceContext;
  
  
  public SearchOrderViewModel(IContext applicationContext)
  {
         InstanceContext = applicationContext;
  
  }
  </code></pre>
  
  <p>My problem is that when the CurrentView changes to the SearchOrderViewModel, its returning System.NullReferenceException: 'Object reference not set to an instance of an object.' from InitializeComponent() in SearchOrderView.xaml which basically is:</p>
  
  <pre><code>public partial class SearchOrderView : UserControl
      {
          public SearchOrderView()
          {
              InitializeComponent(); &lt;----- Exception happens here
          }
      }
  </code></pre>
  
  <p>Stack Trace if helps:</p>
  
  <pre><code> at System.DefaultBinder.BindToMethod(BindingFlags bindingAttr, MethodBase[] match, Object[]&amp; args, ParameterModifier[] modifiers, CultureInfo cultureInfo, String[] names, Object&amp; state)
     at MS.Internal.Xaml.Runtime.DynamicMethodRuntime.CreateInstanceWithCtor(Type type, Object[] args)
     at MS.Internal.Xaml.Runtime.DynamicMethodRuntime.CreateInstanceWithCtor(XamlType xamlType, Object[] args)
     at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
     at MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.CreateInstance(XamlType xamlType, Object[] args)
     at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
     at System.Xaml.XamlObjectWriter.WriteEndObject()
     at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
     at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
     at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
     at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
     at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
     at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
  </code></pre>
  
  <p>I cant figure out on my own whats wrong with it and spend few hours, but I am out of ideas. I am hoping someone can provide some information whats going wrong ?</p>
  
  <p>EDIT:: If I remove public IContext applicationContextSearch from OrderViewModel(IContext applicationContext) is working, but I cannot pass my application context...</p>