Windows Presentation Foundation Integration Guide

Simple Injector can build up Window classes with their dependencies. Use the following steps as a how-to guide:

Step 1:

Change the App.xaml markup by removing the StartUri property:

<Application x:Class="SimpleInjectorWPF.App"
    <!-- Remove the StartupUri property, start the application from a static Main -->

Step 2:

Add a Program.cs file to your project to be the new entry point for the application:

using System;
using System.Windows;
using SimpleInjector;

static class Program
    static void Main() {
        var container = Bootstrap();

        // Any additional other configuration, e.g. of your desired MVVM toolkit.


    private static Container Bootstrap() {
        // Create the container as usual.
        var container = new Container();

        // Register your types, for instance:
        container.Register<IQueryProcessor, QueryProcessor>(Lifestyle.Singleton);
        container.Register<IUserContext, WpfUserContext>();

        // Register your windows and view models:


        return container;

    private static void RunApplication(Container container) {
        try {
            var app = new App();
            var mainWindow = container.GetInstance<MainWindow>();
        } catch (Exception ex) {
            //Log the exception and exit

Step 3:

Change the ‘Startup object’ in the properties of your project to be the newly created Program class:

'Startup object' settings in the application's properties


Constructor injection can now be used in any window (e.g. MainWindow) and view model:

using System.Windows;

public partial class MainWindow : Window {
    public MainWindow(MainWindowViewModel viewModel) {

        // Assign to the data context so binding can be used.
        base.DataContext = viewModel;

public class MainWindowViewModel {
    private readonly IQueryProcessor queryProcessor;
    private readonly IUserContext userContext;

    public MainWindowViewModel(IQueryProcessor queryProcessor,
        IUserContext userContext) {
        this.queryProcessor = queryProcessor;
        this.userContext = userContext;

    public IEnumerable<IUser> Users => this.queryProcessor.Execute(new GetAllUsers());