For some reason I can't get this to work at all. I have read from various sources that I can override OnStartup in a WPF application and it will fire off as the App is created. However, no matter what I do, nothing is happening. Here is the code.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
// My code goes here, but nothing ever happens.
base.OnStartup(e);
}
}
Obviously I am missing something. Sadly the MSDN page doesn't offer much insight either. http://msdn.microsoft.com/en-us/library/system.windows.application.onstartup.aspx
What am I doing wrong?
EDIT:
It turns out that my problem was a small typo in the namespace. App.xaml.cs had the class defined as 'RTDMyApp.App' and the App.xaml file was referring to it as 'RTD_MYApp.App' At any rate, this fact, combined with the accepted answer below has gotten me back on track.
OnStartup raises the Startup event. A type that derives from Application may override OnStartup. The overridden method must call OnStartup in the base class if the Startup event needs to be raised.
Did you remove the StartupUri too from the App xaml?
If you did you have to create the window you want show:
base.OnStartUp(e);
Window1 w = new Window1();
this.MainWindow = w;
w.Show();
I think what you really want to do is to subscribe to the Startup
event. You can do this in your XAML file:
<Application ... Startup="Application_Startup">
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