Skip to content

WPF integration recipe - maybe there's a better way? #46

Description

@heltonbiker

I have read the WPF Integration docs and they tell me to do it like this (compact version shown):

static class Program
{
    [STAThread]
    static void Main()
    {
        var container = Bootstrap();
        var app = new App();
        var mainWindow = container.GetInstance<MainWindow>();
        app.Run(mainWindow);
    }
}

On the other hand, a StackOverflow guruThe Man recommends to override the App.OnStartup event:

The appropriate entry point depends on the framework:

  • In console applications it's the Main method
  • In ASP.NET MVC applications it's global.asax and a custom IControllerFactory
  • In WPF applications it's the Application.OnStartup method
  • In WCF it's a custom ServiceHostFactory

etc.
(you can read more about framework-specific Composition Roots in chapter 7 of my book.)

I am, thus, using Simple Injector like this, with the same visible behaviour, and - IMO - a lot less invasiveness:

// after removing StartupUri from App.xaml...
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        var bootstrapper = new BootStrapper();
        MainWindow = bootstrapper.GetMainWindow();
        MainWindow.Show();
    }       
}

Besides some minor variations, the whole point is the idea of not bypassing the normal WPF App instantiation, but instead just to override OnStartup event.

What do you think of changing the documentation to reflect this?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions