I have 2 WPF windows. App.xaml.cs opnes the First Window and read some data while showing the status and then close it. Then App.xaml.cs opens the second window. When I debug code execute correctly but after closing the first window it close down the entire application. What am I doing wrong? Is it not possible in App.xaml.cs ?
Here is the code. (For this test I am using code behind instead MVVM) In this code I have put a button to close the first window.
App.xaml.cs:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
TestWindow tw = new TestWindow();
bool? rslt = tw.ShowDialog();
if (rslt == true)
{
MainWindow mw = new MainWindow();
mw.Show(); //I am not sure why the Application close itself
}
}
}
TestWindow.xaml:
<Window x:Class="Shell.Startup.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestWindow" Height="300" Width="300">
<Grid>
<Button x:Name="ButtonYes" Content="Yes" HorizontalAlignment="Left" Height="21" Margin="95,192,0,0" VerticalAlignment="Top" Width="66" RenderTransformOrigin="1.485,0.81" Click="ButtonYes_Click"/>
</Grid>
</Window>
TestWindow.xaml.cs:
public partial class TestWindow : Window
{
public TestWindow()
{
InitializeComponent();
}
private void ButtonYes_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
}
MainWindow.xaml:
<Window x:Class=" Shell.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
NOTE:
I also tried the Application_Startup as given in the answer here.