title: Dependency injection with Unity on wpf MVVM application
body: <p>I have been trying to set up dependency injection in a wpf application using Unity, but can't seem to fully understand how the views and viewmodels should be set up.</p>
  
  <p>Have looked into another SO post --> <a href="https://stackoverflow.com/questions/13480387/wpf-with-unity-container-how-to-register-and-resolve-viewmodels-to-views">Wpf Unity</a> but can't seem to understand it quite yet. I have used Unity before, but just in a MVC application, so I know how to inject it in the contructors.</p>
  
  <p>Here is my views and viewModels in the application.</p>
  
  <p><strong>Views:</strong></p>
  
  <pre><code>MainWindow.xaml
  BookingView.xaml
  ContactDetailsView.xaml
  ReservationsView.xaml
  </code></pre>
  
  <p><strong>ViewModels:</strong></p>
  
  <pre><code>MenuViewModel (MainWindow uses this viewModel)
  BookingViewModel
  ContactViewModel
  ReservationsViewModel
  </code></pre>
  
  <p>My ViewModels all have Interfaces implemented, like IMenuViewModel, should the view also have an interface?</p>
  
  <p>I guess that since the MainWindow is the starting point, it should be here to register the container right?</p>
  
  <h1>Update:</h1>
  
  <p>Have found something, but not sure if I have done it right. Here is what I have done so far!</p>
  
  <p>1: Using startup method in app.cs</p>
  
  <pre><code>public partial class App : Application
  {
      protected override void OnStartup(StartupEventArgs e)
      {
          base.OnStartup(e);
          IUnityContainer container = new UnityContainer();
  
          container.RegisterType&lt;IViewMainWindowViewModel, MainWindow&gt;();
          container.RegisterType&lt;IViewMainWindowViewModel, MenuViewModel&gt;();
  
          var mainWindow = container.Resolve&lt;MainWindow&gt;(); // Creating Main window
          mainWindow.Show();
      }
  
  }
  </code></pre>
  
  <p>2: Remove uri from start up.</p>
  
  <p>3: Make IViewMainWindowViewModel interface in MainWindow class, the interface is empty.</p>
  
  <pre><code>public interface IViewMainWindowViewModel
  {
  
  }
  </code></pre>
  
  <p>4: Make a reference to this interface in the MainWindow</p>
  
  <pre><code>public partial class MainWindow : Window, IViewMainWindowViewModel
  {
      public MainWindow(IViewMainWindowViewModel viewModel)
      {
          InitializeComponent();
          DataContext = viewModel;
      }
  </code></pre>
  
  <p>5: Also for the MenuViewModel</p>
  
  <pre><code>public class MenuViewModel : IViewMainWindowViewModel
  {
      Code not shown!
  }
  </code></pre>
  
  <p>This will not even start the application..</p>
  
  <h1>Update 2</h1>
  
  <p>My MainWindow class look like this:</p>
  
  <pre><code>public interface IViewMainWindowViewModel
  {
  
  }
  
  public partial class MainWindow : Window, IViewMainWindowViewModel
  {
      public MainWindow(IViewMainWindowViewModel viewModel)
      {
          InitializeComponent();
          DataContext = viewModel;
      }
  </code></pre>
  
  <p>App class now look like this:</p>
  
  <pre><code>protected override void OnStartup(StartupEventArgs e)
      {
          base.OnStartup(e);
          IUnityContainer container = new UnityContainer();
          container.RegisterType&lt;IViewMainWindowViewModel, MainWindow&gt;();
          container.RegisterType&lt;IViewMainWindowViewModel, MenuViewModel&gt;();
          container.Resolve&lt;MainWindow&gt;().Show();
          //Do the same actions for  all views and their viewmodels
      }
  </code></pre>
  
  <p>I get an exception on this line when running the application</p>
  
  <pre><code>container.Resolve&lt;MainWindow&gt;().Show();
  </code></pre>
  
  <h1>Update 3</h1>
  
  <p>In my MenuViewModel it has two command which will open two views, do I then need to inject those views in the MenuViewModel's constructor or can you just make another empty interface between MenuViewModel and BookingView as an example?</p>