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?
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With