我正在制作一个动态歌词播放器,使用question答案中的ItemsControl
.
我使用两个相互叠置的ItemsControls
,它们都放置在不同的Canvas
中(以获取当前显示的文本的实际宽度),Canvas
放置在Grid
中,以确保Canvas
在窗口中显示在正确的位置,如下所示:
<Grid>
<Canvas x:Name="MainBackLineCanvas" Grid.Column="0" Grid.Row="0">
<ItemsControl x:Name="MainBackLine" ItemsSource="{Binding}">
...
</ItemsControl>
</<Canvas>
<Canvas x:Name="MainColorLineCanvas" Grid.Column="0" Grid.Row="0">
<ItemsControl x:Name="MainColorLine" ItemsSource="{Binding}">
...
</ItemsControl>
</Canvas>
</Grid>
The effect of this window is as follows:
MainBackLine
(蓝色文本)将在开头完全显示,MainColorLine
(黄色文本)的宽度在开头设置为0,宽度将随着歌曲时间的增加而增加.
我用DispatcherTimer
来改变它的宽度.我的代码如下:
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
的宽度将非常缓慢而不平滑地变化.我try 过改变DispatcherTimer
的Interval
,或使用DoEvents
,但没有任何效果.
请问有什么办法使它更平滑些吗.