Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# WPF App.Current becomes null after custom dialog window closes

Tags:

c#

wpf

I have WPF application where I have changed the default entry point.

<Application x:Class="FrazerClient.App" Startup="AppStartup">

public void AppStartup(object sender, StartupEventArgs e)
{
    // Does some minor work before an application window opens.
}

The minor work calls this a couple of times:

App.Current.Dispatcher.Invoke((Action)delegate
{
  // Custom dialog window is opened
});

The second time this is called, App.Current becomes null. I am almost positive is has to do with the custom dialog window closing, but not really sure how to prevent the closing of the dialog window from nulling out App.Current when the last window closes.

This also prevents App.Current.Shutdown() from working.

like image 444
David Bentley Avatar asked Jan 20 '26 06:01

David Bentley


1 Answers

Set ShutDownMode to OnExplicitShutdown so that closing the window does not automatically shutdown the application.

You usually do that in the Xaml of the application object

<Application x:Class="FrazerClient.App"
         Startup="AppStartup"
         ShutdownMode="OnExplicitShutdown">
like image 71
NineBerry Avatar answered Jan 23 '26 14:01

NineBerry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!