提问者:小点点

C#WPF如何在加载xaml窗口时显示我的progressbar?


我正在尝试在我的WPF窗口加载时显示我的进度条。我成功地填充了progressbar,但是它没有显示。参考屏幕截图来查看我的意思是显示progressbar,但是当窗口已经完全加载时,它会显示窗口的每个组件都加载了。当我在每个方法的末尾放一些MessageBox来加载像这个屏幕截图和这个屏幕截图这样的组件时,它正在做我想要的正确行为,所以我的问题是如何在加载xaml窗口时显示我的progressbar?如有任何帮助,我们将不胜感激。

提前致谢

   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");

    }

我在每个调用来加载窗口的方法中都像这样填充进度条


共1个答案

匿名用户

这是你设定的顺序,我想这样排序应该可以解决这个问题:

 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();
}