我在 wpf 的 StatusBar 中有一个要更新的文本框。
我在 ListBox 中有一个文件列表。在每个文件上,我都会通过调用方法 ProcessFile() 来执行一些操作。因此,每当文件处理完成时,我想在 StatusBar 文本中显示该文件的名称。
我尝试过这样的事情:
private void button_Click(object sender, RoutedEventArgs e)
{
statusBar.Visibility = Visibility.Visible;
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(TimeConsumingMethod), frame);
Dispatcher.PushFrame(frame);
statusBar.Visibility = Visibility.Collapsed;
}
public object TimeConsumingMethod(Object arg)
{
((DispatcherFrame)arg).Continue = false;
foreach (string fileName in destinationFilesList.Items)
{
txtStatus.Text = fileName.ToString();
//Assume that each process takes some time to complete
System.Threading.Thread.Sleep(1000);
}
return null;
}
但我只能在状态栏中看到最后一个文件的名称。代码有什么问题?我该如何纠正?