Closing WPF MainWindow
开发者_JAVA技巧I have a login dialog being displayed in a WPF application that has a cancel button on it that instead attempting to log into the application, closes it down. The problem is, I have a user control that appears on the mainwindow form, so if I attempt to run the Application.Current.Shutdown() or this.Close() methods, I get an exception from one of the event handlers assigning an event to a specific control on the user control. The exception is a NullReferenceException, i.e, object reference not set to instance of an object.
From my limited understanding of WPF, it would appear as if, while I'm attempting the shut the application down, there is another thread (the UI thread?) attempting to initialise the components on MainWindow. I'm not sure whether or not this is correct, but I was hoping someone here could shed light on why its doing this and any means of stopping it so I can shut the application down properly.
Further information, this login dialog and the subsequent Application.Current.Shutdown() method is all taking place within the MainWindow contructor, dunno if that helps.
Thanks for the help.
I suspect that you do not remove event handlers before disposing main window, that might cause this exception.
Okay nevermind, I got the answer, I placed the InitialiseComponent() method around an if statement and set the bool to false if the user clicked exit, Happy days :)
精彩评论