您可以使用擴展WPF工具箱中的忙指標:here。
然後,您需要在另一個線程中執行處理(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;
};
當實現我得到錯誤調用線程必須是STA,因爲許多UI組件需要這個。 [STAThread]屬性放在哪裏? – user1765862