4

I have a WPF project were I am using multiple WPF windows. My WPF Windows are:

  • MainWindow
  • Window1
  • Login

I have to case scenarios, in the first one everything works fine but in the second I get a null reference exception.

  1. First Scenario: App.xaml is configured so as startup window to be MainWindow.

When user clicks on Button on MainWindow he is forwarded in Window1 were I have the following code:

MainWindow obj=(MainWindow)Application.Current.MainWindow;
 private void button1_Click(object sender, RoutedEventArgs e)
        {
        obj.checkBox1.IsChecked = false;
        }

2.Second Scenario: App.xaml is configured so as startup window to be Login Window. Code in Login:

private void button1_Click(object sender, RoutedEventArgs e)
        {
        var window=new MainWindow();
        window.Show();
        this.Close();
        }

In this scenario when I click on button in Window1 a null reference exception is thrown for obj.

What is the difference in the initialization of MainWindow in these 2 cases that causes the exception in the 2nd case and how can I overcome it?

2 Answers 2

5
+50

Well, the first Window being opened when you start the application, will become the window you get back when calling Application.Current.MainWindow.

In your case, that is Login, but in Window1 you expect it to be MainWindow, which is wrong. Since Login has been closed, you get null back, and the app crashes.

To fix this you have to make MainWindow the MainWindow :-)

You can do that in Login like so:

var window = new MainWindow();
Application.Current.MainWindow = window;
window.Show();
this.Close();
Sign up to request clarification or add additional context in comments.

1 Comment

I think think this solves my problem..wait till tommorow when I will get back to work to test it and I will accept your answers as far as the problem is solved... thank you for the reply
0

The this.Close() on your Login window in scenario 2 will close the application as this is the window being pointed to in your app.xaml file as the startup window. See the MainWindow property

MainWindow is automatically set with a reference to the first Window object to be instantiated in the AppDomain.

In the first scenario you don't close the MainWindow so the application continues. In the second one you close the Login window so the application exits.

In the first scenario you don't show where the user is forwarded to window1. It would be helpful to see that code as well.

2 Comments

this.Close() closes Login Window
I've had the problem where I close my first window before my second is displayed, causing the application to shutdown. Two options to avoid this behavior that I've used. 1) Change the Application.ShutdownMode property to OnExplicitShutdown. 2) Use Dispatcher.BeginInvoke() to queue up a delegate at a very low priority right after you call Show() on the new window that calls Close() on the first window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.