您可以使用扩展 WPF 工具包中的繁忙指示器:here http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator.
然后你需要进行处理(LoadData
)在另一个线程中以避免冻结 UI。为此,简单的方法是使用后台工作者:
向 wm 添加一个布尔值以指示应用程序何时繁忙:
private bool isBusy;
public bool IsBusy
{
get { return isBusy; }
set
{
this.isBusy = value;
this.OnPropertyChanged("IsBusy");
}
}
设置后台工作者:
private void LoadData(string searchBy)
{
IsBusy = true;
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (o, ea) =>
{
//Do your heavy work here
};
worker.RunWorkerCompleted += (o, ea) =>
{
IsBusy = false;
};
worker.RunWorkerAsync();
}
如果您想显示当前处理进度的进度条,您需要做更多的工作。首先,您需要将自定义样式应用于忙碌指示器:
<toolkit:BusyIndicator IsBusy="True" BusyContent="Processing..." >
<extToolkit:BusyIndicator.ProgressBarStyle>
<Style TargetType="ProgressBar">
<Setter Property="IsIndeterminate" Value="False"/>
<Setter Property="Height" Value="15"/>
<Setter Property="Margin" Value="8,0,8,8"/>
<Setter Property="Value" Value="{Binding ProgressValue}"/>
</Style>
</extToolkit:BusyIndicator.ProgressBarStyle>
</toolkit:BusyIndicator>
然后将 ProgressValue 属性添加到您的虚拟机:
private int progressValue ;
public int ProgressValue
{
get { return progressValue ; }
set
{
this.progressValue = value;
this.OnPropertyChanged("ProgressValue ");
}
}
并从后台工作人员报告进度:
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += (o, ea) =>
{
//Do your heavy work here
worker.ReportProgress(10);
//Do your heavy work here
worker.ReportProgress(20);
//And so on
};
worker.ProgressChanged += (o,ea) =>
{
ProgressValue = e.ProgressPercentage;
};
worker.RunWorkerCompleted += (o, ea) =>
{
IsBusy = false;
};