c# WPF 如何在 xaml window 加载时显示我的进度条?

c# WPF How to show my progressbar while the xaml window is loading?

我正在尝试在我的 WPF window 加载时显示我的进度条。我成功填充了进度条,但它没有显示 请参阅屏幕截图了解我的意思 showingProgressBar but when the windows has loaded completly, it's showing when every components of the window is loaded. It's doing the right behavior i want when I put some MessageBox at the end of every method to load components like this screenshot and this screenshot,所以我的问题是如何在加载 xaml window 时显示我的进度条? 任何帮助将不胜感激。

提前致谢

   private void DoWorkButton_Click(object sender, RoutedEventArgs e)
       {

             testProgressBar.Visibility = Visibility.Visible;
              ProgressTextblock.Visibility = Visibility.Visible;
               BackgroundWorker worker = new BackgroundWorker();
               worker.RunWorkerCompleted += worker_RunWorkerCompleted;
                worker.WorkerReportsProgress = true;
                worker.DoWork += worker_doWork;
                worker.ProgressChanged += worker_ProgressChanged;
                 worker.RunWorkerAsync();
    }

    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        testProgressBar.Value = e.ProgressPercentage;
        ProgressTextblock.Text = (string)e.UserState;
    }

    private void worker_doWork(object sender, DoWorkEventArgs e)
    {
        var worker = sender as BackgroundWorker;
        worker.ReportProgress(0, String.Format("Chargement des composants de la fenetre"));



    }

    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Fenetre chargée normalement");

    }

我在调用加载的每个方法中填充进度条 like this window

这是你设置顺序的方式,我认为这样排序应该可以解决问题:

 private void DoWorkButton_Click(object sender, RoutedEventArgs e)
   {
         testProgressBar.Visibility = Visibility.Visible;
         ProgressTextblock.Visibility = Visibility.Visible;
         BackgroundWorker worker = new BackgroundWorker();
         worker.WorkerReportsProgress = true; 
         worker.DoWork += worker_doWork;                         
         worker.ProgressChanged += worker_ProgressChanged;
         worker.RunWorkerCompleted += worker_RunWorkerCompleted;
         worker.RunWorkerAsync();
}