如何使用WPF进度栏

如何使用WPF进度栏

本文介绍了如何使用WPF进度栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将IsIndeterminate属性设置为true的WPF进度条控件.我的问题是,它没有更新.

I am trying to use the WPF progressbar control with the IsIndeterminate property set to true.The problem I have, is that it doesn't get updated.

我正在做这样的事情:

pbProgressBar.Visibility = Visibility.Visible;
//do time consuming stuff
pbProgressBar.Visibility = Visibility.Hidden;

我试图将其包装在线程中,然后使用Dispatcher对象进行分派.我该如何解决这个问题:).

I tried to wrap this in a thread and then dispatch it with the Dispatcher object.How should I solve this problem :).

推荐答案

您必须在后台线程上执行耗时的工作,并且必须确保未将 Visibility 设置回隐藏,直到之后后台线程完成了它的工作.基本过程如下:

You must do the time consuming stuff on a background thread, and you must ensure that the Visibility isn't set back to Hidden until after the background thread has done its thing. The basic process is as follows:

private void _button_Click(object sender, RoutedEventArgs e)
{
   _progressBar.Visibility = Visibility.Visible;

   new Thread((ThreadStart) delegate
   {
       //do time-consuming work here

       //then dispatch back to the UI thread to update the progress bar
       Dispatcher.Invoke((ThreadStart) delegate
       {
           _progressBar.Visibility = Visibility.Hidden;
       });

   }).Start();
}

这篇关于如何使用WPF进度栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:56