1

I have a small issue with my WPF application.

I have a splash image (as a XAML Window) and the main app (as another XAML window that gets caleld from Splash)

Even when I use this .Close() on Splash window and work on Main app, the Splash window is still visible in taskbar.

If I were to use this .Close on main app I would have two blank windows in taskbar application that I have to press X to close completely.

I've tried with Application.Current.Shutdown() as well but results were same.

Splash:

    public Splash()
    {
        InitializeComponent();
    }

    private void Window_ContentRendered(object sender, EventArgs e)
    {
        CloseProcedure(); 
    }

    public async void CloseProcedure()
    {
        //Missing data loading which will be async.            
        UC_Main UCMain = new UC_Main();
        MainWindow window = new MainWindow();
        window.AppContent.Children.Add(UCMain);
        this.Close();
        await Task.Delay(500); //for fade effect.
        window.Show();
    }

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

Main App

        private void BTN_Close_MouseUp(object sender, MouseButtonEventArgs e)
    {
        this.Close();
    }

    private void titleBar_MouseDown(object sender, MouseButtonEventArgs e)
    {
        titleBar.Background = Brushes.White;
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        Closing -= Window_Closing;
        e.Cancel = true;
        var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(0.2));
        this.BeginAnimation(UIElement.OpacityProperty, anim);
        anim.Completed += (s, _) => Application.Current.Shutdown();
    }
}
Donald Jansen
  • 1,937
  • 4
  • 22
  • 41
DethoRhyne
  • 930
  • 1
  • 9
  • 29

2 Answers2

0

The default shutdown mode of a WPF app is "OnLastWindowClose" which means as soon as the last window is closed, the applicaton shutdown procedure will be called, so you should not need to explicitly write System.Windows.Application.Shutdown().

You probably have something referencing the splash screen that isn't being closed off. When you open your main window, you should close the splash screen properly at that time and ensure that any reference to it is null.

If you post all your code, it will be easier to know the correct fix.

To prevent a window showing in the taskbar in the first place, set the ShowInTaskbar property of your Window XAML declaration:

<Window ShowInTaskbar="False" ...
James Harcourt
  • 6,017
  • 4
  • 22
  • 42
  • Added code. Trying out ShowInTaskbar="False" bandaged the issue around splash window being visible, but the issue isn't fixed as closing Main window still leaves a blank app in taskbar and process running. – DethoRhyne Dec 04 '15 at 14:22
  • How are you opening splash in the first place? From which place, and via which method? – James Harcourt Dec 04 '15 at 18:00
  • it is the default startup page defined in app.xaml. – DethoRhyne Dec 05 '15 at 18:08
0

I've come up with a solution for this. Instead of trying to shut down the process and animate the fade inside the closing event handler, I wrote an asynchronous method that will fade out the screen, await for the same amount of time and then kill the process. Works like a charm.

    private void BTN_Close_MouseUp(object sender, MouseButtonEventArgs e)
    {
        this.FadeOut();
    }

    async private void FadeOut()
    {
        var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(0.2));
        this.BeginAnimation(UIElement.OpacityProperty, anim);
        await Task.Delay(200);
        this.Close(); 
        System.Diagnostics.Process.GetCurrentProcess().Kill();
    }
DethoRhyne
  • 930
  • 1
  • 9
  • 29