.NET WPF-Fenster mit FadeIn und FadeOut-Animation
Unten ist code-snippets ein Fenster mit FadeIn und FadeOut-animation:
//Create the fade in storyboard
fadeInStoryboard = new Storyboard();
fadeInStoryboard.Completed += new EventHandler(fadeInStoryboard_Completed);
DoubleAnimation fadeInAnimation = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(0.30)));
Storyboard.SetTarget(fadeInAnimation, this);
Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
fadeInStoryboard.Children.Add(fadeInAnimation);
//Create the fade out storyboard
fadeOutStoryboard = new Storyboard();
fadeOutStoryboard.Completed += new EventHandler(fadeOutStoryboard_Completed);
DoubleAnimation fadeOutAnimation = new DoubleAnimation(1.0, 0.0, new Duration(TimeSpan.FromSeconds(0.30)));
Storyboard.SetTarget(fadeOutAnimation, this);
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(UIElement.OpacityProperty));
fadeOutStoryboard.Children.Add(fadeOutAnimation);
Unten sind die helper-Methoden, die das auslösen der animation:
///<summary>
///Fades the window in.
///</summary>
public void FadeIn()
{
//Begin fade in animation
this.Dispatcher.BeginInvoke(new Action(fadeInStoryboard.Begin), DispatcherPriority.Render, null);
}
///<summary>
///Fades the window out.
///</summary>
public void FadeOut()
{
//Begin fade out animation
this.Dispatcher.BeginInvoke(new Action(fadeOutStoryboard.Begin), DispatcherPriority.Render, null);
}
Der code funktioniert Super bis auf zwei Probleme:
- Auf FadeIn() das Fenster beginnt mit einem hässlichen schwarzen hintergrund dann korrekt animiert.
- Auf FadeOut() korrekt animiert dann das Fenster endet mit einem hässlichen schwarzen hintergrund.
Warum ist das passiert? Wie mache ich diese animation flüssig laufen, ohne den schwarzen hintergrund reibungslos?
Du musst angemeldet sein, um einen Kommentar abzugeben.
Müssen Sie
AllowsTransparency
zutrue
auf dieWindow
für Sie gekommen, um sich aus vollständig transparent.Leider ist dies nur möglich mit
WindowStyle=None
, so würden Sie haben, um Ihre eigene Titelleiste.Diese bringt einige Fiese performance-Probleme, da das Fenster nicht in hardware gerendert mehr. Wenn Sie diesen Weg gehen, empfehle ich die Einstellung
RenderOptions.ProcessRenderMode
zuRenderMode.SoftwareOnly
(.NET 4.0 oder höher) auf dem UI-thread, um akzeptable performance in einfachen Kompositionen.