Evg*_*y E 1 .net c# wpf performance lag
我有两个动画:
<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>
Run Code Online (Sandbox Code Playgroud)
以及开始它们的代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
在用户和音乐内容之间进行一些切换后,ChangeLayout动画开始非常缓慢且滞后,并根据WPF Performance Suite FPS动画切换,从500 +切换到最大4eh ...无法找到任何解决方案=(
为了我的英语,我到目前为止正在学习.
您是在每次按下按钮时添加事件处理程序而不是删除它们.
hl.Completed += (_sender, _e) =>
{
};
Run Code Online (Sandbox Code Playgroud)
在两个按钮处理程序中.这将耗尽资源,并且意味着您每按一次按钮就会多次调用代码.
解决方案是在按钮点击之外移动处理程序设置,或者在完成后移除处理程序.在后一种情况下,您必须将事件处理程序代码移动到单独的方法中,以便执行此操作:
hl.Completed += MyEventHandler;
Run Code Online (Sandbox Code Playgroud)
然后:
private void MyEventHandler(object sender, EventArgs e)
{
// Do stuff
hl.Completed -= MyEventHandler;
}
Run Code Online (Sandbox Code Playgroud)
虽然这意味着hl两种方法都必须可见.
| 归档时间: |
|
| 查看次数: |
973 次 |
| 最近记录: |