Reputation: 145
i have two animation:
<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>
And code that begin they:
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();
}
After a few switches between user and content of music, ChangeLayout animation begins very slow and laggy and animated according to the WPF Performance Suite FPS animation after switching drops from 500 + to a maximum of 4eh ... Can't find any solution for this =(
Sry for my english, i am studying so far.
Upvotes: 1
Views: 1138
Reputation: 137148
You are adding event handlers on every button click and not removing them.
hl.Completed += (_sender, _e) =>
{
};
in both button handlers. This will both eat up resources and mean that you are calling the code multiple times with every button click.
The solution is to either move the handler set up outside the button clicks or remove the handler when you've done. In the latter case you'll have to move your event handler code into a separate method so you can do this:
hl.Completed += MyEventHandler;
Then:
private void MyEventHandler(object sender, EventArgs e)
{
// Do stuff
hl.Completed -= MyEventHandler;
}
Though this means that hl
has to be visible to both methods.
Upvotes: 5