我正在使用 wpf UserControl 替换 AutoCAD 选定图形文件中的文件中的文本。wpf 控件将显示一个状态(ProgressBar),指示在任何给定时间处理的文件数。所以我提出了以下代码,但 ProgressBar 根本没有显示任何进度。这是相关代码的一部分。
XAML:
<ProgressBar HorizontalAlignment="Stretch" Name="pgrSearch" Minimum="0" Maximum="{Binding Path=ProgressBarMaximum}"
Value="{Binding Path=ProgressBarCurrent}" Height="20" Margin="10" />
代码隐藏:
public partial class ReplaceUserControl : UserControl, INotifyPropertyChanged {
public ReplaceUserControl() {
InitializeComponent();
this.DataContext = this;
}
....
private int _progressBarMaximum;
public int ProgressBarMaximum {
get { return _progressBarMaximum; }
set { _progressBarMaximum = value; RaisePropertyChanged("ProgressBarMaximum"); }
}
private int _progressBarCurrent;
private int ProgressBarCurrent {
get { return _progressBarCurrent; }
set { _progressBarCurrent = value; RaisePropertyChanged("ProgressBarCurrent"); }
}
private void ReplaceTextInFiles() { //Called from Button_Click Handler
....
ProgressBarMaximum = filesList.Count - 1;
SearchReplaceWorker replaceWorker = new SearchReplaceWorker(); //The Work Horse
replaceWorker.FileProcessed += new FileProcessedEventHandler(worker_FileProcessed); //Raised by Work Horse when each file is processed
BackgroundWorker workerThread = new BackgroundWorker(); //The Background Worker Thread
workerThread.DoWork += (o, e) => {
replaceWorker.ReplaceTextInFiles(SearchText, ReplaceText, filesList, ReportFolderPath, MatchCase, MatchSubstring);
};
workerThread.RunWorkerAsync(); //Start the Background Thread Async
}
void worker_FileProcessed(object sender, EventArgs e) {
ProgressBarCurrent = ProgressBarCurrent + 1; //Update the ProgressBar status
}
当 ProgressBarCurrent 递增时,为什么 ProgressBar 不自行更新,如代码中所示。
编辑: 为了处理 UI 线程上的 ProgressBar 更新代码,我将代码更改为使用 BackgroundWorker.ReportProgress(),如下所示。
用户控件的代码隐藏:
private void ReplaceTextInFiles() { //Called from Button_Click()
if (!Directory.Exists(SearchFolderPath)) {
MessageBox.Show("Invalid Directory Selected for Search");
return;
}
if (!Directory.Exists(ReportFolderPath)) {
MessageBox.Show("Invalid Directory Selected for Report File");
return;
}
List<string> filesList = null;
try {
if (LookInSubFolders) {
filesList = Directory.GetFiles(@SearchFolderPath, "*.dwg", SearchOption.AllDirectories).ToList();
}
else {
filesList = Directory.GetFiles(@SearchFolderPath, "*.dwg", SearchOption.TopDirectoryOnly).ToList();
}
}
catch (Exception ex) {
MessageBox.Show("Error Occurred getting the files list. Contact Admin");
}
pgrSearch.Visibility = Visibility.Visible;
ProgressBarMaximum = filesList.Count - 1;
SearchReplaceWorker replaceWorker = new SearchReplaceWorker();
BackgroundWorker workerThread = new BackgroundWorker();
workerThread.WorkerReportsProgress = true;
workerThread.ProgressChanged += (o, e) => { //This event handler gets called correctly.
ProgressBarCurrent++;
};
workerThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(workerThread_RunWorkerCompleted);
workerThread.DoWork += (o, e) => {
replaceWorker.ReplaceTextInFiles(workerThread, SearchText, ReplaceText, filesList, ReportFolderPath, MatchCase, MatchSubstring);
};
workerThread.RunWorkerAsync();
}
背景工作者:
public void ReplaceTextInFiles(BackgroundWorker workerThread, string searchText, string replaceText, List<string> filesList, string reportPath,
bool MatchCase, bool MatchSubstring) {
...
workerThread.ReportProgress(50);
}
ProgressBar 仍然不会自行更新。