在App.xaml.cs的OnStartup中关闭第一个窗口后,再打开C#WPF第二个窗口 - c#

我有2个WPF窗口。 App.xaml.cs选择“第一个窗口”,并在显示状态时读取一些数据,然后将其关闭。然后App.xaml.cs打开第二个窗口。当我调试代码正确执行但关闭第一个窗口后,它将关闭整个应用程序。我究竟做错了什么? App.xaml.cs中不可能吗?

这是代码。 (对于该测试,我在后面使用代码而不是MVVM)在此代码中,我放置了一个按钮以关闭第一个窗口。

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>

注意:

我还尝试了答案here中给出的Application_Startup。

参考方案

如下更改App.xaml中的“应用程序关闭”模式。请注意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>

然后,您在App类中的onStartup方法应为

    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();
    }

最后,由于更改了关机模式,因此我们必须明确关闭该应用程序。因此,您的MainWindow应具有以下代码

/// <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();
    }
}

如何重用WPF用户控件但更改绑定属性? - c#

我有一个WPF用户控件,它使用我的viewmodel中的属性进行绑定。现在,我想使用具有相同视图模型的该用户控件的两个实例,但覆盖其中一个的绑定。代码看起来像这样用户控制<UserControl x:Class="TestWpfApp.UserControl1" xmlns="http://schemas.microsof…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…

尝试引用用户控件时出现错误“名称在当前上下文中不存在” - c#

我想在我的应用程序中标准化弹出窗口。因此,我为其创建了一个用户控件,并按如下所示引用它: <!--<Popup x:Name="LoginPopup" Grid.ColumnSpan="2" Grid.RowSpan="2" Height="768" Width=&#…