最近做一個項目,看到以前同事寫的進度條效果不錯,所以,拿來簡化了下,不炫,但是項目中還是夠用的。
還是,先來看下呼叫以後的效果
1、因為ProgressbBar的Foreground顯示不得不一樣,所以,要有一個參數去給控制項進行設置,因此定義了一個參數值ForegroundColor
public int ForegroundColor {get{return _foregroundColor; }set{ _foregroundColor = value; LinearGradientBrush lgb = dictionary["ForegroundColor" + value] as LinearGradientBrush;if (lgb != null) proBar.Foreground = txt.Foreground = percent.Foreground = lgb; } }
##程式碼裡有這麼一句話「LinearGradientBrush lgb = dictionary["ForegroundColor" + value] as LinearGradientBrush;」是為了方便通過這是這個參數去樣式檔裡取樣式的。
<LinearGradientBrush x:Key="ForegroundColor1" EndPoint="1,0.5" StartPoint="0,0.5"><GradientStop Color="#FFBBF586" Offset="0.5"/><GradientStop Color="#FFD4F9C3" Offset="1"/></LinearGradientBrush><LinearGradientBrush x:Key="ForegroundColor2" EndPoint="1,0.5" StartPoint="0,0.5"><GradientStop Color="#FF5BE26E" Offset="0.5"/><GradientStop Color="#FF8DEC9C" Offset="1"/></LinearGradientBrush><LinearGradientBrush x:Key="ForegroundColor3" EndPoint="1,0.5" StartPoint="0,0.5"><GradientStop Color="#FFB656F2" Offset="0.5"/><GradientStop Color="#FFAE8DFE" Offset="1"/></LinearGradientBrush><LinearGradientBrush x:Key="ForegroundColor4" EndPoint="1,0.5" StartPoint="0,0.5"><GradientStop Color="#FF3AE9E9" Offset="0.5"/><GradientStop Color="#FF8DFDFE" Offset="1"/></LinearGradientBrush>
2、既然是ProgressBar就要有一個進度值,這個值,我們用TextBlock來進行顯示,一定要實現通知接口,這樣,才能保證實時的通知到頁面上。
public string ValueText {get{return _valueText; }set{ _valueText = value;if (this.PropertyChanged != null) {this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ValueText")); } } }
3、啟用一個後台線程,來不斷的更新進度效果
private void Bgw_DoWork(object sender, DoWorkEventArgs e) {for (int i = 0; i < BarValue; i++) { System.Threading.Thread.Sleep(50); proBar.Dispatcher.Invoke(new Action( delegate{if (proBar.Value <= BarValue) { proBar.Value++; } })); ValueText = i + ""; } ValueText = BarValue + ""; }
原始碼
以上是WPF實作簡單的進度條怎麼做?的詳細內容。更多資訊請關注PHP中文網其他相關文章!