WPF-在启动进度条显示完成之前加载主窗口,完成后启动窗口不会关闭

时间:2018-09-24 22:06:21

标签: c# wpf progress-bar splash-screen

我创建了一个初始窗口和主窗口。我想在打开主窗口之前加载启动屏幕,并显示进度的滚动条。

我已经成功创建了一个首先加载的启动窗口,进度条显示进度,但是主窗口在进度条完成之前加载,并且进度完成后启动屏幕不会关闭。我的app.xaml.cs中有以下代码

任何帮助将不胜感激!

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        Splash splash = new Splash();
        splash.Show();
        MainWindow main = new MainWindow();
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.DoWork += worker_DoWork;
        worker.ProgressChanged += worker_ProgressChanged;
        worker.RunWorkerAsync();
    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            (sender as BackgroundWorker).ReportProgress(i);
            Thread.Sleep(i);
        }
        foreach (System.Windows.Window swindow in System.Windows.Application.Current.Windows)
        {
            (swindow as Splash).Close();
        }
    }

    public void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        int progressstatus = e.ProgressPercentage;
        foreach (System.Windows.Window window in System.Windows.Application.Current.Windows)
        {
            if (window.GetType() == typeof(Splash))
            {
                (window as Splash).prgProgress.Value = progressstatus;
            }
        }
    }
}

0 个答案:

没有答案