简体   繁体   中英

override OnStartup in WPF

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.

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">

Sequence sequence sequence. How annoying.

The right sequence (for a WPF application with NO Main method explicitly developed/declared) is:

// XAML
...       Startup="Application_Startup"

//code-behind
private void Application_Startup(object sender, StartupEventArgs e)
{
 ...
 ...
 // do something.  In fact, here I do a lot of stuff that reflects 
 // some serious recent application illnesss:
try
        {
              //http://connect.microsoft.com/VisualStudio/feedback/details/618027/uriformatexception-thrown-by-ms-internal-fontcache-util
            System.Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"));

            // per http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.ietflanguagetag(v=vs.110).aspx

            var cultureName = CultureInfo.CurrentCulture.Name;
            FrameworkElement.LanguageProperty.OverrideMetadata(
                typeof(FrameworkElement),
                new FrameworkPropertyMetadata(
                    XmlLanguage.GetLanguage(cultureName)));


            // Setup unhandled exception handlers
            #region Handlers For Unhandled Exceptions
            // anything else to do on startup can go here and will fire after the base startup event of the application
            // First make sure anything after this is handled
            // Creates an instance of the class holding delegate methods that will handle unhandled exceptions.
            CustomExceptionHandler eh = new CustomExceptionHandler();

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(eh.OnAppDomainException);

            // this ensures that any unhandled exceptions bubble up to a messagebox at least
            Dispatcher.CurrentDispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(eh.OnDispatcherUnhandledException);

            #endregion  Handlers For Unhandled Exceptions


            // Start the dispatcher
            // for Galasoft threading and messaging

            DispatcherHelper.Initialize();

        }
        catch (Exception ex)
        {
            ex.PreserveExceptionDetail();
            throw ex;
        }
}

and then I do:

protected override void OnStartup(StartupEventArgs e)
    {
        App.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
        App.HasRaisedFatalException = false;

        base.OnStartup(e);

        try
        {



            //Force just one copy to run
            this.ForceSingleInstance();

...
...
...
}

and so far the patient is feeling much better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM