1
votes

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();
    }
}
2
try Environment.Exit(0);Mainak
check this stackoverflow.com/questions/9992119/…user5308950
@Mainak Didn't work. Same thingDethoRhyne

2 Answers

0
votes

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" ...
0
votes

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();
    }