title: NullReferenceException in SimpleInjector.Container.Verify when instancing view via XAML
body: <p>I'm still in the process of getting into WPF and also decided to give Simple Injector a spin. I created a very simple test project based on the <a href="https://simpleinjector.readthedocs.io/en/latest/wpfintegration.html" rel="nofollow noreferrer">WPF integration example provided in the Simple Injector documentation</a>. My current code is pretty much identical to that example or arguably even simpler as I do not yet have any service objects.</p>
  
  <p>What is different is that I do have a simple View with an accompanying ViewModel that is currently the only thing that is on the <code>MainWindow</code>:</p>
  
  <pre><code>    &lt;Window x:Class="WpfPlayground.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:local="clr-namespace:WpfPlayground"
              xmlns:view="clr-namespace:WpfPlayground.View"
              mc:Ignorable="d"
              Title="MyApp" Height="450" Width="800"&gt;
          &lt;Grid&gt;
              &lt;view:MyView /&gt;
          &lt;/Grid&gt;
      &lt;/Window&gt;
  </code></pre>
  
  <p>My <code>MainWindowViewModel</code> class exists but is currently still empty. Its constructor has no parameters.</p>
  
  <p>This is what I have so far of <code>MyView</code> (the XAML is still just an empty <code>&lt;Grid /&gt;</code>):</p>
  
  <pre><code>public partial class MyView : UserControl
  {
      public MyView(MyViewModel myViewModel)
      {
          InitializeComponent();
          DataContext = myViewModel;
      }
  }
  </code></pre>
  
  <p>This is the <code>Bootstrap()</code> method from my <code>Program</code> class:</p>
  
  <pre><code>        private static Container Bootstrap()
          {
              var container = new Container();
  
              container.Register&lt;MainWindow&gt;();
              container.Register&lt;MainWindowViewModel&gt;();
              container.Register&lt;MyView&gt;();
              container.Register&lt;MyViewModel&gt;();
  
              container.Verify();
  
              return container;
          }
  </code></pre>
  
  <p>Now, I get a <code>NullReferenceException</code> inside the <code>MainWindow</code> constructor when <code>InitializeComponent()</code> is called ("Object reference not set to an instance of an object"). The call stack points back to the call to <code>container.Verify()</code> in <code>Program.Bootstrap()</code> (see above). The constructors of neither <code>MyViewModel</code> nor <code>MyView</code> are ever reached.</p>
  
  <p>If I were to take a guess I'd say that the application is not going through the <code>Container</code> to get the instance of <code>MyView</code>. One of the reasons I did this experiment was actually because I was interested to see how Simple Injector would accomplish this as the documentation seemed to imply that this would somehow happen automatically. Could it be there is a step missing from the documentation? Something to register some sort of interceptor? Is this maybe not the right way to use Simple Injector with WPF/XAML after all? Hopefully I won't have to create controls in code?</p>