【问题标题】:override OnStartup in WPF覆盖 WPF 中的 OnStartup
【发布时间】:2011-06-10 12:46:23
【问题描述】:

由于某种原因,我根本无法让它工作。我从各种来源中读到,我可以在 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”无论如何,这个事实,再加上下面接受的答案,让我回到了正轨.

【问题讨论】:

  • 如果你用“step into”命令开始你的VS,会发生什么?
  • 尝试将代码放在 base.OnStartup(e);
  • 应用程序是否启动正常(除了您的代码没有运行)或您是否遇到异常?另外,您是否尝试过在 app.xaml 中明确地重新启动启动事件?
  • 我在 VS 2010 中尝试了一个快速的 WPF 项目,我可以很好地覆盖 OnStartup。如果您同时发布项目中的 App.xaml 和 App.xaml.cs 文件,我可能会为您提供帮助。
  • @Ingo:如果 OnStartup 从未被调用,这应该如何改变?

标签: c# wpf startup


【解决方案1】:

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

如果你必须创建你想要显示的窗口:

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

【讨论】:

    【解决方案2】:

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

    <Application ... Startup="Application_Startup">
    

    【讨论】:

    • 嗯...我认为这里确实有问题。如果我在“新”应用程序上执行此操作,一切都很好。另一方面,我当前的应用程序将不再编译,告诉我它无法找到“Application_Startup”,即使它已明确定义。
    • 处理事件不同于覆盖虚拟方法
    • @Erno:我知道处理事件不同于覆盖虚拟方法。但是在这种特定情况下呢?真正的区别是什么?
    • app.xaml 可能有一些问题,没有将 x:Class beeeing 设置为正确的值,您可以发布您的 app.xaml 吗?
    • @A.R. xaml.cs 文件中的类名是否与 xaml 文件中 x:Class 中的名称匹配?
    【解决方案3】:

    序列序列序列。真烦人。

    正确的顺序(对于具有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();
    
    ...
    ...
    ...
    }
    

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

    【讨论】:

      猜你喜欢
      • 2022-12-18
      • 1970-01-01
      • 2021-01-24
      • 2011-01-23
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      相关资源
      最近更新 更多