覆盖WPF中的OnStartup

时间:2011-06-10 12:46:24

标签: c# wpf startup

出于某种原因,我根本无法使用它。我从各种来源读到我可以在WPF应用程序中覆盖OnStartup,它会在创建应用程序时触发。然而,无论我做什么,都没有发生任何事情。这是代码。

public partial class App : Application
{

    protected override void OnStartup(StartupEventArgs e)
    {
      // My code goes here, but nothing ever happens.

      base.OnStartup(e);
    }
}

显然我错过了一些东西。可悲的是,MSDN页面也没有提供太多的见解。 http://msdn.microsoft.com/en-us/library/system.windows.application.onstartup.aspx

我做错了什么?

编辑:
事实证明,我的问题是命名空间中的一个小错误。 App.xaml.cs将类定义为'RTDMyApp.App',App.xaml文件将其称为'RTD_MYApp.App'无论如何,这个事实加上下面接受的答案让我重回正轨

3 个答案:

答案 0 :(得分:18)

您是否从App xaml中删除了StartupUri?

如果你这样做,你必须创建你想要显示的窗口:

base.OnStartUp(e);
Window1 w = new Window1();
this.MainWindow = w;
w.Show(); 

答案 1 :(得分:12)

我认为你真正想做的是订阅Startup事件。您可以在XAML文件中执行此操作:

<Application ... Startup="Application_Startup">

答案 2 :(得分:-2)

序列序列。多么烦人。

正确的序列(对于明确开发/声明的 NO Main方法的WPF应用程序)是:

// 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;
        }
}

然后我做:

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

...
...
...
}
到目前为止,患者感觉好多了。