Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override OnStartup in WPF

Tags:

c#

wpf

startup

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.

like image 254
A.R. Avatar asked Jun 10 '11 12:06

A.R.


People also ask

What is WPF OnStartup?

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.


2 Answers

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(); 
like image 163
Emond Avatar answered Sep 22 '22 06:09

Emond


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">
like image 20
Daniel Hilgarth Avatar answered Sep 20 '22 06:09

Daniel Hilgarth