简体   繁体   中英

Opening C# WPF second Windows after closing First Window in OnStartup of App.xaml.cs

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 .

Change the Application Shutdown mode in App.xaml as below. Note the ShutdownMode="OnExplicitShutdown"

<Application x:Class="WpfApplication2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             ShutdownMode="OnExplicitShutdown">
    <Application.Resources>

    </Application.Resources>
</Application>

Then your onStartup Method in App class shall be

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        TestWindow t = new TestWindow();
        bool? res = t.ShowDialog();
        if (res == true)
        {
            MainWindow mw = new MainWindow();
            mw.Show();
        }
        else
            this.Shutdown();
    }

And finally we have to shutdown the app explicitly since we changed the shutdown mode to be so. Hence your MainWindow shall have the following code

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Closed += MainWindow_Closed;
    }

    void MainWindow_Closed(object sender, EventArgs e)
    {
        App.Current.Shutdown();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM