Fading out a window

前端 未结 3 1858
余生分开走
余生分开走 2020-12-08 05:35

I am currently developing a wpf c# application. I have added to event triggers to the xaml of the form to fade in when the window loads and fades out when the window closes.

3条回答
  •  清歌不尽
    2020-12-08 06:16

    Unloaded is not a suitable event for this, i'm not sure if this event can even occur for windows. You need to handle Closing, prevent it from actually closing, start an animation and close it when the Completed event of the animation occurs.

    e.g.

    
    
    private void Window_Closing(object sender, CancelEventArgs e)
    {
        Closing -= Window_Closing;
        e.Cancel = true;
        var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(1));
        anim.Completed += (s, _) => this.Close();
        this.BeginAnimation(UIElement.OpacityProperty, anim);
    }
    

提交回复
热议问题