override OnStartup in WPF

前端 未结 3 1608
无人共我
无人共我 2021-01-17 17:08

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 cre

相关标签:
3条回答
  • 2021-01-17 17:31

    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.

    0 讨论(0)
  • 2021-01-17 17:47

    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(); 
    
    0 讨论(0)
  • 2021-01-17 17:52

    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">
    
    0 讨论(0)
提交回复
热议问题