当我尝试更改wpf中的项目范围的宽度时,UI性能问题
我正在使用itemscontrol
从此答案中制作一个动态歌词播放器待办事项播放 - wpf中的弦乐中的特征“>问题。
我使用两个彼此堆叠的 itemscontrols ,两者都放在不同的canvas
中(要获取当前显示的文本的实际宽度),>画布
放置在网格
中,以确保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>
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
的宽度将非常缓慢而不会平稳。我已经尝试更改dispatchertimer
's Interval
,或使用doevents
,但无效。
有什么方法可以使它更加顺畅。
I'm making a dynamic lyrics player using the ItemsControl
from the answer to this question.
I use two ItemsControls
that are stacked on top of each other and both are placed in different Canvas
(to get the actual width of the text currently being displayed), Canvas
are placed in the Grid
to ensure that the Canvas
will appear in the correct position in the window, like this:
<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:
The MainBackLine
(the blue text) will be fully displayed at the beginning, the width of the MainColorLine
(the yellow text) is set to 0 at the beginning, and the width will increase as the song time increases.
I used a DispatcherTimer
to change its width. My code is as follows:
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;
}
When I run this code, the width of the MainColorLine
will change very slowly and not smoothly. I've tried changing the DispatcherTimer
's Interval
, or using DoEvents
, but nothing works.
Is there any way to make it smoother please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用二倍度来使宽度动画,例如:
Try to use a DoubleAnimation to animate the width, e.g.: