WPF动画性能随着时间的推移而下降
本文关键字:时间 动画 性能 WPF | 更新日期: 2023-09-27 18:11:01
我有两个动画:
<Storyboard x:Key="ChangeLayout">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.Left)" Storyboard.TargetName="currentContent">
<EasingDoubleKeyFrame KeyTime="0:0:0.0" Value="900"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="HideLayout">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.Left)" Storyboard.TargetName="currentContent">
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="-900">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
和它们开头的代码:
private void btnUser_Click(object sender, RoutedEventArgs e)
{
if (currentContent.Content != null)
if (currentContent.Content.GetType() == typeof(Layouts.User))
return;
((hl.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames[0] as EasingDoubleKeyFrame).Value = -this.ActualWidth;
hl.Completed += (_sender, _e) =>
{
currentContent.Content = new Layouts.User();
cl.Completed += (ssender, ee) =>
{
btnMusic.Opacity = 0.5;
btnUser.Opacity = 0.9;
};
cl.Begin();
};
hl.Begin();
}
private void btnMusic_Click(object sender, RoutedEventArgs e)
{
if (currentContent.Content != null)
if (currentContent.Content.GetType() == typeof(Layouts.Music))
return;
((hl.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames[0] as EasingDoubleKeyFrame).Value = -this.ActualWidth;
hl.Completed += (_sender, _e) =>
{
if (Layouts.Music.CurrentMusic == null)
{
Layouts.Music.CurrentMusic = new Layouts.Music();
Layouts.Music.CurrentMusic.GetMusic();
}
currentContent.Content = Layouts.Music.CurrentMusic;
cl.Completed += (ssender, ee) =>
{
btnUser.Opacity = 0.5;
btnMusic.Opacity = 0.8;
};
cl.Begin();
};
hl.Begin();
}
在用户和音乐内容之间进行几次切换后,ChangeLayout动画开始很慢很慢,并且根据WPF Performance Suite FPS动画切换后从500 +下降到最大4eh…找不到任何解决办法=
对不起我的英语,我一直在学习。
你是在每次点击按钮时添加事件处理程序,而不是删除它们。
hl.Completed += (_sender, _e) =>
{
};
在两个按钮处理程序中。这不仅会消耗资源,而且意味着每次点击按钮都要多次调用代码。
解决方案是将处理程序设置移到按钮单击之外,或者在完成后删除处理程序。在后一种情况下,您必须将事件处理程序代码移动到一个单独的方法中,以便您可以这样做:hl.Completed += MyEventHandler;
:
private void MyEventHandler(object sender, EventArgs e)
{
// Do stuff
hl.Completed -= MyEventHandler;
}
虽然这意味着hl
必须对两个方法都可见