显示问题的 WPF 进度条
本文关键字:WPF 问题 显示 | 更新日期: 2023-09-27 18:36:49
我在显示进度条值时遇到问题。我想要,该进度条使用其他线程来更新他的值,但它仅在 ParseTpSheet 方法之后执行此操作。我希望,你会帮助我。谢谢。
namespace IntegrationPlanning.Controls
{
/// <summary>
/// Логика взаимодействия для PlaningParserControl.xaml
/// </summary>
[Serializable]
public partial class PlaningParserControl : UserControl
{
private BackgroundWorker worker = new BackgroundWorker();
private readonly PlaningParserModel _parserModel = new PlaningParserModel();
public PlaningParserControl()
{
InitializeComponent();
worker.WorkerReportsProgress = true;
worker.ProgressChanged += worker_ProgressChanged;
worker.DoWork += worker_DoWork;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
for (var i = 0; i < 100; i++)
{
Thread.Sleep(150);
worker.ReportProgress(i);
}
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ParseProgressBar.Value = e.ProgressPercentage;
}
private void IncProgressBar(int value)
{
ParseProgressBar.Value += value;
}
private void AddFileButton_OnClick(object sender, RoutedEventArgs e)
{
var excelApplication = new Excel.Application();
var openFileDialog = new OpenFileDialog {Filter = "excel files(*.xls)(*.xlsx)|*.xls;*.xlsx" };
openFileDialog.ShowDialog();
if (openFileDialog.FileName.EndsWith(".xls") || openFileDialog.FileName.EndsWith(".xlsx"))
{
ParseProgressBar.Value = 0;
worker.RunWorkerAsync();
_parserModel.Workbook = excelApplication.Workbooks.Add(openFileDialog.FileName);
_parserModel.ParseTpSheet();
}
else
{
MessageBox.Show("Файл не был выбран", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
}
excelApplication.Quit();
}
}
}
非常感谢@Haukinger。结果代码在这里:
namespace IntegrationPlanning.Controls
{
/// <summary>
/// Логика взаимодействия для PlaningParserControl.xaml
/// </summary>
[Serializable]
public partial class PlaningParserControl : UserControl
{
public int ParseProgressBarValue { get; set; }
private BackgroundWorker worker = new BackgroundWorker();
private readonly PlaningParserModel _parserModel = new PlaningParserModel();
public PlaningParserControl()
{
InitializeComponent();
worker.WorkerReportsProgress = true;
worker.ProgressChanged += worker_ProgressChanged;
worker.DoWork += worker_DoWork;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
for (var i = 0; i < 100; i++)
{
Thread.Sleep(150);
worker.ReportProgress(i);
}
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ParseProgressBar.Value = e.ProgressPercentage;;
}
private void AddFileButton_OnClick(object sender, RoutedEventArgs e)
{
var excelApplication = new Excel.Application();
var openFileDialog = new OpenFileDialog {Filter = "excel files(*.xls)(*.xlsx)|*.xls;*.xlsx" };
openFileDialog.ShowDialog();
if (openFileDialog.FileName.EndsWith(".xls") || openFileDialog.FileName.EndsWith(".xlsx"))
{
ParseProgressBar.Value = 0;
worker.RunWorkerAsync();
var t = Task.Run(() =>
{
_parserModel.Workbook = excelApplication.Workbooks.Add(openFileDialog.FileName);
_parserModel.ParseTpSheet();
});
}
else
{
MessageBox.Show("Файл не был выбран", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
}
excelApplication.Quit();
}
}
}