tags:

views:

339

answers:

3

My brain is all over the map trying to fully understand Unity right now. So I decided to just dive in and start adding it in a branch to see where it takes me. Surprisingly enough (or maybe not), I am stuck just getting my darn Application to load properly.

It seems like the right way to do this is to override OnStartup in App.cs. I've removed my StartupUri from App.xaml so it doesn't create my GUI XAML. My App.cs now looks something like this:

public partial class App : Application
{
    private IUnityContainer container { get; set; }

    protected override void OnStartup(StartupEventArgs e)
    {
        container = new UnityContainer();
        GUI gui = new GUI();
        gui.Show();
    }

    protected override void OnExit(ExitEventArgs e)
    {
        container.Dispose();
        base.OnExit(e);
    }
}

The problem is that nothing happens when I start the app! I put a breakpoint at the container assignment, and it never gets hit.

What am I missing? App.xaml is currently set to ApplicationDefinition, but I'd expect this to work because some sample Unity + WPF code I'm looking at (from Codeplex) does the exact same thing, except that it works!

I've also started the app by single-stepping, and it eventually hits the first line in App.xaml. When I step into this line, that's when the app just starts "running", but I don't see anything (and my breakpoint isn't hit). If I do the exact same thing in the sample application, stepping into App.xaml puts me right into OnStartup, which is what I'd expect to happen. Argh!

I've also just created a new WPF application from scratch, removed StartupUri, overrode OnStartup(), and it also works. WTH?

Is it a Bad Thing to just put the Unity construction in my GUI's Window_Loaded event handler? Does it really need to be at the App level?

A: 

Your problem doesn't seem related to Unity at all... Make sure that :

  • the startup object is set to YourProject.App (in the project properties page)
  • the build action for App.xaml is set to "Page"

Otherwise, I can't see any reason why it shouldn't work...


Update : just another idea... Try to set the StartupUri back to App.xaml, and call the base implementation in OnStartup :

protected override void OnStartup(StartupEventArgs e)
{
    container = new UnityContainer();
    base.OnStartup(e);
}
Thomas Levesque
you're right, this doesn't have anything to do with Unity, it's just that I was in a Unity mindset (and was originally writing a different Unity question at the time). I should change that.
Dave
App.xaml is currently set to ApplicationDefinition. The weird thing is that I'm looking at a Unity code sample from Codeplex, and it is set exactly the same way, except that it works! The author of that source removed the StartupUri and simply overrode OnStartup, and his application works as desired. Ugh!
Dave
see my updated answer
Thomas Levesque
I'm confused, did you mean GUI.xaml, where GUI.xaml is my initial Window that I want displayed when the application starts? I can't set it to App.xaml, because I'll just get the error "Cannot create more than one System.Windows.Application instance in the same AppDomain." Anyhow, if I use GUI.xaml, OnStartup never gets called.
Dave
A: 

Double check that the x:Class in App.xaml in the same namespace/class as in your App.xaml.cs. It's easy to copy/paste from another project and to forget to modify this.

If for any reason you don't manage to solve this, remove the App.xaml and add a Main() which does a new App().Run(). If this doesn't work either, there's something really odd here.

Julien Lebosquain
the namespace and class are correct -- I am Unity-fying my current app, so all I am doing right now is changing the manner in which it starts up. I might try changing App.xaml to Page like Thomas had suggested, and then adding Main. But I would think this should work if the sample works! Driving me crazy. :)
Dave
A: 

I'm giving up on this for now and am just going to register everything with Unity in my GUI's constructor. This app is getting thrown out and written from scratch again anyway, and I just need to have my shared libraries converted from MEF to Unity so I can get started on the new app. Thanks for the help, everyone.

Dave