I have a WPF project, I use several WPF windows. My WPF windows:
- Mainwindow
- Window1
- To come in
I have case scripts, in the first one everything works fine, but in the second I get an exception with a null reference.
- First scenario: App.xaml is configured so that the launch window is
MainWindow .
When the user clicks the Button on MainWindow button, he is sent to Window1 if 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 that the launch window is the login window. Login Code:
private void button1_Click(object sender, RoutedEventArgs e) { var window=new MainWindow(); window.Show(); this.Close(); }
In this case, when I click the button in Window1, a null reference exception is thrown for obj.
What is the difference in initializing MainWindow in these two cases, which causes an exception in the second case, and how can I overcome it?
source share