开发者

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 pr开发者_StackOverflowoblem 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);
var window = new Window1();
this.MainWindow = window;
window.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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜