进度条从 0% 跳到 100%,中间不更新
Progress bar skips from 0% to 100% without updating in between
我正在实现一个 ProgressBar,它在每个完成的任务(总共 5 个任务)后更新。它应该在每个完成的任务后更新 20%。单击按钮会启动 运行 任务,但是当单击按钮时,进度条会从 0% 变为 100%,中间不会更新。 Thread.Sleep(1000) 添加在进度值的每个增量之前,以模拟每个任务将花费的时间。在为每个任务添加代码之前,我想让进度条正常工作。
我已经尝试添加 AvaloniaPropertyChanged 事件,但它似乎并没有改变问题。
MainWindow.xaml:
<ProgressBar Name="RunProgress" Value="{Binding Progress}" IsIndeterminate="False" Minimum="0" Maximum="100" Height="30"/>
<TextBlock Text="{Binding ElementName=RunProgress, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" />
MainWindow.xaml.cs:
//boolean variables for whether or not that process is completed (false if not done, true if done)
bool MasterLimitsRead = false;
bool MasterOrganized = false;
bool LimitsOrganized = false;
bool RemovedFailed = false;
bool OutputCreated = false;
//holds descriptions of the 5(+1) operations the program runs through including description for Done
string[] operation =
{
"Reading the master and limits file text",
"Organizing the master data",
"Organizing the limits",
"Identifying and removing failed devices",
"Creating the output",
"Done"
};
context.CurrentOp = operation[0];
Thread.Sleep(1000);
MasterLimitsRead = true;
if(MasterLimitsRead == true)
{
context.Progress += 20;
context.CurrentOp = operation[1];
}
Thread.Sleep(1000);
MasterOrganized = true;
if(MasterOrganized == true)
{
context.Progress += 20;
context.CurrentOp = operation[2];
}
Thread.Sleep(1000);
LimitsOrganized = true;
if(LimitsOrganized == true)
{
context.Progress += 20;
context.CurrentOp = operation[3];
}
Thread.Sleep(1000);
RemovedFailed = true;
if(RemovedFailed == true)
{
context.Progress += 20;
context.CurrentOp = operation[4];
}
Thread.Sleep(1000);
OutputCreated= true;
if(OutputCreated== true)
{
context.Progress += 20;
context.CurrentOp = operation[5];
}
MainWindowViewModel.cs:
private string currentOp = string.Empty; //variable to store current operation, initialized to empty string
public string CurrentOp
{
get => currentOp;
set
{
if (value != currentOp)
{
currentOp = value;
OnPropertyChanged();
}
}
}
private string progress = 0; //variable to store ProgressBar value in percent, initialized to 0
public string CurrentOp
{
get => currentOp;
set
{
if (value != currentOp)
{
currentOp = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if(PropertyChanged != null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
预期:进度条应在每个任务完成后从 0% 增加到 100%,增加 20%(由 Thread.Sleep(1000) 延迟 1 秒模拟)
实际:进度条从 0% 开始,然后在单击按钮时暂停 UI 交互 5 秒,然后将进度条更新为 100%。我希望它在进度条的每次增量值时更新。
通过调用 Sleep
,您将阻塞 UI 线程。如果 UI 线程被阻塞,将不会有任何 UI 更新。要模拟 long-running 任务,请改用 await Task.Delay(TimeSpan.FromSeconds(1))
。
我正在实现一个 ProgressBar,它在每个完成的任务(总共 5 个任务)后更新。它应该在每个完成的任务后更新 20%。单击按钮会启动 运行 任务,但是当单击按钮时,进度条会从 0% 变为 100%,中间不会更新。 Thread.Sleep(1000) 添加在进度值的每个增量之前,以模拟每个任务将花费的时间。在为每个任务添加代码之前,我想让进度条正常工作。
我已经尝试添加 AvaloniaPropertyChanged 事件,但它似乎并没有改变问题。
MainWindow.xaml:
<ProgressBar Name="RunProgress" Value="{Binding Progress}" IsIndeterminate="False" Minimum="0" Maximum="100" Height="30"/>
<TextBlock Text="{Binding ElementName=RunProgress, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" />
MainWindow.xaml.cs:
//boolean variables for whether or not that process is completed (false if not done, true if done)
bool MasterLimitsRead = false;
bool MasterOrganized = false;
bool LimitsOrganized = false;
bool RemovedFailed = false;
bool OutputCreated = false;
//holds descriptions of the 5(+1) operations the program runs through including description for Done
string[] operation =
{
"Reading the master and limits file text",
"Organizing the master data",
"Organizing the limits",
"Identifying and removing failed devices",
"Creating the output",
"Done"
};
context.CurrentOp = operation[0];
Thread.Sleep(1000);
MasterLimitsRead = true;
if(MasterLimitsRead == true)
{
context.Progress += 20;
context.CurrentOp = operation[1];
}
Thread.Sleep(1000);
MasterOrganized = true;
if(MasterOrganized == true)
{
context.Progress += 20;
context.CurrentOp = operation[2];
}
Thread.Sleep(1000);
LimitsOrganized = true;
if(LimitsOrganized == true)
{
context.Progress += 20;
context.CurrentOp = operation[3];
}
Thread.Sleep(1000);
RemovedFailed = true;
if(RemovedFailed == true)
{
context.Progress += 20;
context.CurrentOp = operation[4];
}
Thread.Sleep(1000);
OutputCreated= true;
if(OutputCreated== true)
{
context.Progress += 20;
context.CurrentOp = operation[5];
}
MainWindowViewModel.cs:
private string currentOp = string.Empty; //variable to store current operation, initialized to empty string
public string CurrentOp
{
get => currentOp;
set
{
if (value != currentOp)
{
currentOp = value;
OnPropertyChanged();
}
}
}
private string progress = 0; //variable to store ProgressBar value in percent, initialized to 0
public string CurrentOp
{
get => currentOp;
set
{
if (value != currentOp)
{
currentOp = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if(PropertyChanged != null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
预期:进度条应在每个任务完成后从 0% 增加到 100%,增加 20%(由 Thread.Sleep(1000) 延迟 1 秒模拟)
实际:进度条从 0% 开始,然后在单击按钮时暂停 UI 交互 5 秒,然后将进度条更新为 100%。我希望它在进度条的每次增量值时更新。
通过调用 Sleep
,您将阻塞 UI 线程。如果 UI 线程被阻塞,将不会有任何 UI 更新。要模拟 long-running 任务,请改用 await Task.Delay(TimeSpan.FromSeconds(1))
。