Reputation: 15016
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?
Upvotes: 7
Views: 6398
Reputation: 375
I had a similar problem -- My App constructor and OnStartup weren't being called (breakpoints not being hit). In case someone else runs across this thread, here's another thing that can go wrong.
I have a solution with several apps, one of which is just a utility I use to manage some XML files. It doesn't get used much, so I basically forgot about it. Originally, my builds were for "Any CPU", but recently I had to change them to "x86" due to a library I was using. Today, I needed to run this utility, but it was crashing and in the debugger nothing in the App class was being called. After checking namespaces (which were OK), I decided to clean the project (as suggested above). When I tried to run the app, I got an error that VS couldn't find the EXE. Apparently, VS was running an old "Any CPU" EXE it found in the output directory which was crashing, and then was deleted when I cleaned the project. I checked the Build | Configuration Manager, and sure enough, I hadn't selected the utility app for the "x86" build. Once I added it, everything worked great. Hope this helps.
Upvotes: 0
Reputation: 292345
Your problem doesn't seem related to Unity at all... Make sure that :
YourProject.App
(in the project properties 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);
}
Upvotes: 2
Reputation: 786
In my case, I hit the "something really odd" part that Julien Lebosquain referred to.
I kept checking and re-checking names, namespaces, event handlers etc and couldn't see anything wrong but still my WPF MainWindow.xaml kept opening first, bypassing the required startup method.
Eventually, I deleted MainWindow.xaml ... but STILL it popped up first. At which point, I cleaned the solution, deleted the binaries from disk (after altering the start-up project) and closed and re-opened the solution.
This 'fixed' my problem.
Upvotes: 1
Reputation: 41213
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.
Upvotes: 8