0

I'm new in WPF, and consider to try DI. I decide to take OnStartup method as Composition Root.

  protected override void OnStartup(StartupEventArgs e)
    {      
        IUnityContainer container = new UnityContainer();
        container.RegisterType<IMailSender, Model.Concrete.GmailSender>();
        var mainWindow = container.Resolve<MainWindow>();
        mainWindow.Show();
    }

But, after the application is started, appears two windows, normal, with my content, and totally empty (seems like not initialized). What is wrong?

2
  • 1
    My guess is you have the Application.StartupUri propery set in App.xaml. You need to remove it. Commented Apr 30, 2015 at 20:03
  • If you are just starting, I would advise taking a look at Prism which includes this DI functionality in the form of MEF or Unity, the real benefit of using an established framework is the amount of documentation out there. Other frameworks with similar functionality are available. Commented Apr 30, 2015 at 20:10

1 Answer 1

3

You need to Remove the StartUri in App.XAML. Also it is good practice to set the application main window. Refer below code.

protected override void OnStartup(StartupEventArgs e)
    {
        IUnityContainer container = new UnityContainer();
        container.RegisterType<IMailSender, Model.Concrete.GmailSender>();
        var mainWindow = container.Resolve<MainWindow>();            
        Application.Current.MainWindow = mainWindow;
        Application.Current.MainWindow.Show();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

If I recall correctly, MainWindow gets set to the first Window displayed automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.