override OnStartup in WPF

僤鯓⒐⒋嵵緔 提交于 2019-12-30 17:24:49

问题


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.


回答1:


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



回答2:


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(); 



回答3:


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.



来源:https://stackoverflow.com/questions/6306549/override-onstartup-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!