在初始化过程中,如何在需要时显示主窗口

时间:2012-06-18 16:39:11

标签: wpf

我正在构建一个WPF应用程序。程序初始化时会显示启动画面。在显示主窗口后,大多数启动序列需要在后台线程中完成。我的问题是,当我想要它时,主窗口不显示。

这是我的主窗口的Window_Loaded事件处理程序:

private void Window_Loaded( object sender, RoutedEventArgs e ) {
    if ( !DoInitialize() ) {
        shuttingDown = true;
        Application.Shutdown();
    }

    e.Handled = true;
}

private bool DoInitialize() {
    if ( !ReadConfiguration( Application.ConfigurationFilePath ) ) {
        return false;
    }

    Thread th = new Thread( new ThreadStart( FinishInitializing ) );
    th.SetApartmentState(ApartmentState.STA); 
    th.Start();

    ClockTimer = new DispatcherTimer( TimeSpan.FromSeconds( 1 ), DispatcherPriority.Background, UpdateClock, Dispatcher );
    ClockTimer.Start();

    return true;
}

正如您所看到的,除了读取配置文件之外,除了启动后台Thread完成初始化并启动DispatcherTimer之外,它没有太大作用。此计时器用于更新显示屏上的时钟。

触发主窗口显示的确切原因是什么?

1 个答案:

答案 0 :(得分:1)

如果您希望在窗口渲染(显示)后执行代码,您可以附加到主窗口上的Window.ContentRendered事件。

要回答您的问题,订单是

  • 显示SplashScreen
  • MainWindow构造函数(InitializeComponent())
  • Window.Loaded名为
  • 显示的窗口
  • Window.ContentRendered名为
  • 隐藏SplashScreen