Fading out a window

前端 未结 3 1854
余生分开走
余生分开走 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:04

    H.B. solution is good but don't close effectively the window because Close() call window_Closing and loop. Here my working solution:

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            AlreadyFaded = false;
        }
    
        bool AlreadyFaded;
        private void window_Closing(object sender, CancelEventArgs e)
        {
            if (!AlreadyFaded)
            {
                AlreadyFaded = true;
                e.Cancel = true;
                var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(1));
                anim.Completed += new EventHandler(anim_Completed);
                this.BeginAnimation(UIElement.OpacityProperty, anim);
            }
        }
    
        void anim_Completed(object sender, EventArgs e)
        {
             Close();
        }
    

提交回复
热议问题