.NET WPF Window FadeIn and FadeOut Animation -
below code snippets of window fadein , fadeout animation:
// create 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 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);
below helper methods trigger animation:
/// <summary> /// fades window in. /// </summary> public void fadein() { // begin fade in animation this.dispatcher.begininvoke(new action(fadeinstoryboard.begin), dispatcherpriority.render, null); } /// <summary> /// fades window out. /// </summary> public void fadeout() { // begin fade out animation this.dispatcher.begininvoke(new action(fadeoutstoryboard.begin), dispatcherpriority.render, null); }
the code works great except 2 problems:
- on fadein() window starts off ugly black background animates correctly.
- on fadeout() animates correctly window ends off ugly black background.
why happening? how make animation run smoothly without black background hitch?
you need set allowstransparency
true
on window
come transparent.
unfortunately possible windowstyle=none
, have implement own title bar.
this brings forth nasty performance issues, since window cannot hardware rendered anymore. if go way suggest setting renderoptions.processrendermode
rendermode.softwareonly
(.net 4.0 or higher) on ui thread acceptable performance in simple compositions.
Comments
Post a Comment