.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:

  1. Auf FadeIn() das Fenster beginnt mit einem hässlichen schwarzen hintergrund dann korrekt animiert.
  2. 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?

InformationsquelleAutor c0D3l0g1c | 2010-09-22
Schreibe einen Kommentar