我正在使用ItemsControlfrom the answer to this 
(MainBackLine藍色文字)開頭會完全顯示,開頭MainColorLine(黃色文字)的寬度設定為0,隨著歌曲時間的增加寬度會增加。
我用 aDispatcherTimer來改變它的寬度。我的代碼如下:
DispatcherTimer TextFlashTimer = new DispatcherTimer();
TextFlashTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
TextFlashTimer.Tick = TextFlash;
private void TextFlash(object? sender, EventArgs e)
{
//playTimePercent is the percentage of the playback progress of the current sentence, which is a Double variable
MainColorLine.Width = ((playTimePercent > 0 && playTimePercent <1) ? playTimePercent : 1) * MainBackLine.ActualWidth;
}
當我運行這段代碼時,寬度的MainColorLine變化會非常緩慢且不平滑。我嘗試更改DispatcherTimer'sInterval或 using DoEvents,但沒有任何效果。
請問有什么辦法可以讓它更流暢。
uj5u.com熱心網友回復:
嘗試使用 DoubleAnimation 為寬度設定影片,例如:
double width = ((playTimePercent > 0 && playTimePercent < 1) ? playTimePercent : 1) * MainBackLine.ActualWidth;
DoubleAnimation doubleAnimation = new DoubleAnimation(width, new Duration(TimeSpan.FromMilliseconds(100)));
MainColorLine.BeginAnimation(FrameworkElement.WidthProperty, doubleAnimation);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/496864.html
