加载表单时,WPF启动画面处于活动状态

时间:2013-09-27 08:29:07

标签: wpf startup

我正在使用带有以下代码的启动画面:

var splashScreen = new SplashScreen("/Resources/enetricity.png");
        splashScreen.Show(false);

        InitializeComponent();
        DataContext = viewModel; 

        // pump until loaded
        PumpDispatcherUntilPriority(DispatcherPriority.Loaded);

        // start a timer, after which the splash can be closed
        var splashTimer = new DispatcherTimer
        {
            Interval = TimeSpan.FromSeconds(2)
        };
        splashTimer.Tick += (s, e) =>
        {
            splashTimer.Stop();
            splashScreen.Close(splashTimer.Interval);               

        };

        splashTimer.Start();

private static void PumpDispatcherUntilPriority(DispatcherPriority dispatcherPriority)
    {
        var dispatcherFrame = new DispatcherFrame();
        Dispatcher.CurrentDispatcher.BeginInvoke((ThreadStart)(() => dispatcherFrame.Continue = false), dispatcherPriority);
        Dispatcher.PushFrame(dispatcherFrame);
    }

但是会发生这种情况:启动画面出现,然后窗口出现,闪屏又回来了,过了一段时间它就消失了。计时器很好,当第二次闪屏时,所有模块和UI都被加载。但我不想看到我的窗户......所以它应该只出现一次

迎接

1 个答案:

答案 0 :(得分:1)

这是另一种方法(可能比你想做的要简单得多):

  1. App.xaml.cs中首先创建并显示启动画面(例如,在Startup事件中)
  2. 创建主窗口并将其传递给初始屏幕实例
  3. 然后关闭主窗口构造函数
  4. 末尾的启动画面